About Question com.enthuware.jfcja.v8.2.279 :

Oracle Certified Foundations Associate Java Certification Questions and Discussion
1Z0-811

Moderator: admin

Post Reply
nick12345
Posts: 17
Joined: Fri Aug 13, 2021 12:43 pm
Contact:

About Question com.enthuware.jfcja.v8.2.279 :

Post by nick12345 »

I'wondering, why a static reference variable "ref" can call a non-static method func()?

Can you please explain?

Thank you :)

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

Re: About Question com.enthuware.jfcja.v8.2.279 :

Post by admin »

Whether the reference is static or not, has no bearing on the object to which that reference it is pointing.

So, even though ref is static, it is pointing to a valid instance of a TestClass instance. So, when you try to do ref.func(), the call is made on that instance of TestClass.

static basically tells you about the relation between the variable and the class in which it is contained. So, here since ref is static, it just means that ref belongs to the class and not to an instance of that class. So, there is only one ref variable. If ref were not static, that would mean that ref belongs to the instance of a class and each instance of the class will have its own independent ref variable.

Since it is such a fundamental concept, I would suggest you to go through a good book to understand this concepts.
If you like our products and services, please help us by posting your review here.

nick12345
Posts: 17
Joined: Fri Aug 13, 2021 12:43 pm
Contact:

Re: About Question com.enthuware.jfcja.v8.2.279 :

Post by nick12345 »

Thank you very much for your great explanation :)

Generally I understand the concept of static / non-static, but this made me confused.
Could you please tell me, what is an advantage of a static reference pointing to an object of a class?

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

Re: About Question com.enthuware.jfcja.v8.2.279 :

Post by admin »

Looking at it from the perspective of advantage / disadvantage is not the right way to look at it. You need to look at it from the perspective of its applicability.
For example, you may have a ApplicationInfo class, where you can have some values such as propertiesFile, like this:
class ApplicationInfo{
public static String propertiesFile = "c:\myapp\abc.properties";
}

Here, I have made propertiesFile static field because I don't want to create any instance of ApplicationInfo to get the location of the properties file. In other classes, I can just use ApplicationInfo.propertiesFile directly (without creating and instance of ApplicationInfo). But the propertiesFile reference itself is pointing to a String object, which is an object of String class.

On the other hand, in a Student class, I would not make the name field static because I know that each student object will have its own name.
class Student{
String name = "dummy"; //not static
}
Here, name is pointing to an object of String class but name itself is not static. This means, I cannot access name field directly like this: Student.name. I need to have an instance of Student and then I can access that instance's name. Like this: new Student().name

So, basically, it really depends on how to visualize your application functionality and how to design it.

You also need to understand that all functionality is ultimately implemented by an instance of a class. How you refer to that instance is entirely up to your application design.
If you like our products and services, please help us by posting your review here.

nick12345
Posts: 17
Joined: Fri Aug 13, 2021 12:43 pm
Contact:

Re: About Question com.enthuware.jfcja.v8.2.279 :

Post by nick12345 »

I'm very grateful for your help! Thank you very much for answering all my questions!

On Sunday I have passed my exam :cheers:

Next year I would like to take the 1Z0-808 exam. I know, I have to learn a lot, but I can't wait 8-)

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

Re: About Question com.enthuware.jfcja.v8.2.279 :

Post by admin »

Congratulations, nick!
Glad to be of help.
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 20 guests