Home arrow Java & J2EE arrow Page 3 - Java Classes

Using Public Methods to Obtain Private Fields - Java

Well it's been a while since we discussed Java and I graced these pages with its complicated beauty (for an example of complicated beauty, take a peek at Hillary Swank, who is pretty in a well...complicated sort of way). In this new series of articles, we will be discussing Classes.

  1. Java Classes
  2. How to Declare a Class
  3. Using Public Methods to Obtain Private Fields
  4. Naming Conventions for Our Pal, the Method
  5. Creating Objects with Constructors
By: James Payne
Rating: starstarstarstarstar / 20
January 09, 2008

print this article



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 & J2EE Articles          >>> More By James Payne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: