Java
  Home arrow Java arrow Page 3 - Java Classes
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java Classes
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2008-01-09

    Table of Contents:
  • Java Classes
  • How to Declare a Class
  • Using Public Methods to Obtain Private Fields
  • Naming Conventions for Our Pal, the Method
  • Creating Objects with Constructors

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    The Web Buyer's Guide is your best source for white papers on a wide range of IT products and services. This Week's Featured White Papers: Taming the Threat Landscape by Symantec

    Java Classes - Using Public Methods to Obtain Private Fields
    (Page 3 of 5 )

    It is common practice to make member fields private. However, sometimes we still need access to the data they contain throughout our program. To retrieve this data, we can use methods, like so:


    public class BruceLee {

    private int punch;

    private int kick;

    private int roundhouse;

    public BruceLee(int doPunch, int doKick, int doRoundhouse) {

    punch = doPunch;

    kick = doKick;

    roundhouse = doRoundhouse;

    }

    public int gimmePunch() {

    return punch;

    }

     

    public void determinePunch(int newPunch)) {

    punch = newPunch;

    }

    }

    public int gimmekick() {

    return kick;

    }

    public void determineKick(int newKick)) {

    kick = newKick;

    }

    }

    public int gimmeRoundhouse() {

    return roundhouse;

    }

    public void determineRoundhouse(int newRoundHouse)) {

    roundhouse = newRoundHouse;

    }

    }

    Declaring Methods

    A method declaration looks like this:


    public double decideWhoIsFatter(double yourWeight, int myWeight) {

    insert math here

    }

    The required parts of a method declaration are the return type, name, some parentheses(), and a body located between two braces{}. There are six possible parts of a method declaration:

    • Modifier: public, private, protected, and package

    • Return type: the returned value's data type, or void if there is no value returned by the method

    • Method name: name of the method

    • Parameter: a list of input parameters, separated by commas, and preceded by data types. They are always enclosed in parentheses, unless there are no parameters, in which case you use two parentheses with nothing in between. 

    • Exception Lists: discussed in a future issue

    • Method Body: the code of the method enclosed between braces.

    More Java Articles
    More By James Payne


       · Hi,Entertaining, easy-to-read article but... I can't help feeling your wording...
       · Oh I nearly forgot, the Bad Non-Default Constructor (BNDC?) isn't that bad at all of...
       · Hey Elinor,Thanks for taking the time to comment. Good points all around,...
     

       

    JAVA ARTICLES

    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections
    - Combating the ‘Object Crisis’




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway