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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

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

Post 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);
    }
}
Last edited by admin on Sun Dec 08, 2013 9:28 pm, edited 2 times in total.
Reason: Please put code inside [code] [/code]

admin
Site Admin
Posts: 10068
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 158 guests