Page 1 of 1

About Question enthuware.ocajp.i.v7.2.832 :

Posted: Sun Dec 08, 2013 10:50 am
by javaman
Hello,

In this class where 'myValue' is a private member, how can I print it's value using dot notation on an intance of the class like this
System.out.println(ct.myValue)
???

Code: Select all

public class ChangeTest {

    private int myValue = 0;
    
    public void showOne(int myValue){
        myValue = myValue;
    }
    
    public void showTwo(int myValue){
        this.myValue = myValue;
    }    
    public static void main(String[] args) {
        ChangeTest ct = new ChangeTest();
        ct.showOne(100);
        System.out.println(ct.myValue);
        ct.showTwo(200);
        System.out.println(ct.myValue);
    }
}

Re: About Question enthuware.ocajp.i.v7.2.832 :

Posted: Sun Dec 08, 2013 9:30 pm
by admin
private means private to the class not to the object. myValue can be accessed anywhere in the class (using a reference to the object, of course). Since main method is within the same class, you can use ct.myValue in that method.

HTH,
Paul.