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

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

Moderator: admin

Post Reply
smahale
Posts: 4
Joined: Sun Oct 29, 2017 7:30 pm
Contact:

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

Post by smahale »

Code: Select all

public class Square {
    private static double side = 0;  // LINE 2
  
    public static void main(String[] args) {   // LINE 4
        Square sq = new Square();  // LINE 5
        side = 10;  // LINE 6
   }
}

If 'side' was declared as static like in the code above, could we have accessed it from the main method, like shown above (instead of creating an object and accessing through the object)

Thanks!

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

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

Post by admin »

Yes, but if you make side variable static, side will not belong to a Square object but to the Square class. You won't need a Square object to access side from a static method.
If you like our products and services, please help us by posting your review here.

smahale
Posts: 4
Joined: Sun Oct 29, 2017 7:30 pm
Contact:

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

Post by smahale »

Thanks. And appreciate your clarification that a Square object will not be necessary. 'Square.side' should do.
However if I do have an object, this static 'side' is also available through the object, correct?

Just to add more context to my question, I got confused because in a class, the private member variables are all accessible through getters and setters, without having to use an object reference. And in our case we had to use an object reference for 'side' in our main method.

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

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

Post by admin »

smahale wrote:Thanks. And appreciate your clarification that a Square object will not be necessary. 'Square.side' should do.
However if I do have an object, this static 'side' is also available through the object, correct?
Corrent.
Just to add more context to my question, I got confused because in a class, the private member variables are all accessible through getters and setters, without having to use an object reference. And in our case we had to use an object reference for 'side' in our main method.
Instance variables (whether public or private, doesn't matter) can ONLY be accessed through a reference to an object of that class. When you try to access instance variables from within an instance method of the same class (for example, from getter or setter methods), you do not have to type the reference to access those variables because an implicit variable named "this" is added by the compiler. So even though you are not typing the reference name, the reference is there. For example, if you have a private instance variable named value in a class, the following two getter methods are equivalent -

Code: Select all

private int getValue(){
   return value; //did not use a reference name here so "this" will be added by the compiler
}

private int getValue(){
   return this.value; 
}
We had to use a reference variable sq to access side because main is a static method. Static methods are not executed in the context of an object and so there is no "this" variable for the compiler to add. That is why you need to tell the compiler which object's side do you want to access.
If you like our products and services, please help us by posting your review here.

smahale
Posts: 4
Joined: Sun Oct 29, 2017 7:30 pm
Contact:

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

Post by smahale »

Thank you so much for the detailed explanation. It makes perfect sense. :)

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 51 guests