Public, private & this

Controlling different variables in different classes.

In the below class, the declaration of;

private int hour;

private int minute;

private int second;

As private, only the methods within the cat.class can use these variables.

//cat.java
public class cat {
	private int hour;
	private int minute;
	private int second;
	public void makeTime (int h, int m, int s){
		// check data inputed is right
		hour = ((h>=0 && h<24)? h : 0);
                minute = ((m>=0 && m<60)? m : 0);
                second = ((s>=0 && s<60)? s : 0);
	}
	// display through military time 12 to 24
	public String militaryTime(){

       // display first one to two decimal places
       // add a colon, display second one to two
       // decimal places (the 02d bit means to
       // two desimal places
       return String.format("%02d:%02d:%02d", hour, minute, second);

       public String toString(){
               return String.format("%d:%02d:%02d %s", ((hour==0||hour==12)?12:hour%12),minute,second,(hour<12?"am":"pm"));
       }
}

We can go and try to use these variables in our other class as below

// jamesprogram.java
class jamesprogram {
	public static void main (String[] args){
		// make a cat object to we can use the methods
		// within the cat class
		cat catObject = new cat();
		catObject.makeTime(14,15,20);
		System.out.println(catObject.militaryTime());
	System.out.println(catObject.toString());
		catObject.hour = 5;
	}
}

… with the code

catObject.hour = 5;

but this shows an error saying “the field cat.hour is not visible”

If we change

private int hour;

private int minute;

private int second;

to

public int hour;

public int minute;

public int second;

Our error will go away.

——————————————————————————

In the following example, the cat class, the java is looking to the local variables first, instead of those in makeTime() method.


// jamesprogram.java
class jamesprogram {

public static void main (String[] args){

// make a cat object to we can use the methods
// within the cat class
cat catObject = new cat();
catObject.makeTime(14,15,20);

System.out.println(catObject.militaryTime());
System.out.println(catObject.toString());
}
}

//cat.java
public class cat {

private int hour=2;
private int minute=3;
private int second=4;

public void makeTime (int hour, int minute, int second){
// check data inputed is right
hour = 6;
minute = 7;
second = 8;
}

// display through military time 12 to 24
public String militaryTime(){
// display first one to two decimal places
// add a colon, display second one to two
// decimal places (the 02d bit means to
// two decimal places
return String.format("%02d:%02d:%02d", hour, minute, second);
}

public String toString(){
return String.format("%d:%02d:%02d %s", ((hour==0||hour==12)?12:hour%12),minute,second,(hour<12?"am":"pm"));

}
}

Outputs:

02:03:04
2:03:04 am

… but what if we want to use the variables
hour = 6;
minute = 7;
second = 8;

from the makeTime() method?

If the method contains the same variables as the local variables the program will always look at the local variables instead.

This can be overpowered though, using the ‘this’ reference, to refer to the variables within the makeTime() method instead.

In the cat.class we now have


public void makeTime (int hour, int minute, int second){
// check data inputed is right
this.hour = 6;
this.minute = 7;
this.second = 8;
}

This kind of translates to “don’t use this (the variables declared within the class file, use ‘this'”

This change, pardon the pun, now gives the output:

06:07:08
6:07:08 am

To complete this demo, I include an updated jamesprogram.class, full code below.


// jamesprogram.java
class jamesprogram {

public static void main (String[] args){

// make a cat object to we can use the methods
// within the cat class
cat catObject = new cat();

System.out.println(catObject.militaryTime());
System.out.println(catObject.toString());

catObject.makeTime(14,15,20);

System.out.println(catObject.militaryTime());
System.out.println(catObject.toString());
}
}

and the cat.class


//cat.java
public class cat {

private int hour=2;
private int minute=3;
private int second=4;

public void makeTime (int hour, int minute, int second){
// check data inputed is right
this.hour = 6;
this.minute = 7;
this.second = 8;
}

// display through military time 12 to 24
public String militaryTime(){
// display first one to two decimal places
// add a colon, display second one to two
// decimal places (the 02d bit means to
// two desimal places
return String.format("%02d:%02d:%02d", hour, minute, second);
}

public String toString(){
return String.format("%d:%02d:%02d %s", ((hour==0||hour==12)?12:hour%12),minute,second,(hour<12?"am":"pm"));

}
}

Gives the output:

02:03:04
2:03:04 am
06:07:08
6:07:08 am

Summary: Any time you want to use the variables in your method, as opposed to those declared in the class file, if they are of the same name, you need to use ‘this’.

Leave a Reply