This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling a Static Variable or Method

 
Greenhorn
Posts: 18
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello fellow programmers!

I am preparing for the OCA Java SE 8 Programmer I exam and to prepare for it I use the book by Jeanne Boyarsky and Scott Selikoff. However, I came upon some confusion. Here:

Calling a Static Variable or Method

Usually, accessing a static member is easy. You just put the classname before the method or variable and you are done. For example:

System.out.println(Koala.count);
Koala.main(new String[0]);


Both of these are nice and easy. There is one rule that is trickier. You can use an instance of the object to call a static method. The compiler checks for the type of the reference and uses that instead of the object—which is sneaky of Java. This code is perfectly legal:

5: Koala k = new Koala();
6: System.out.println(k.count); // k is a Koala
7: k = null;
8: System.out.println(k.count); // k is still a Koala


Believe it or not, this code outputs 0 twice. Line 6 sees that k is a Koala and count is a static variable, so it reads that static variable. Line 8 does the same thing. Java doesn’t care that k happens to be null. Since we are looking for a static, it doesn’t matter.

Above is a part of the book.

The problem is that I still don't get why the code outputs 0 twice. Please explain with detail.

Thanks,

Maxwell
 
Marshal
Posts: 28126
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was going to say that line 6 outputs "0" and then line 8 outputs "0", which adds up to twice. But I see you posted something which already says that. So I'm not clear on what part of it is confusing you.
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maxwell Xu wrote:
The problem is that I still don't get why the code outputs 0 twice. Please explain with detail.



The first zero is from line 6. The second zero is from line 8.

Henry
 
Maxwell Xu
Greenhorn
Posts: 18
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but wasn't Koala assigned to null?
 
Henry Wong
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maxwell Xu wrote:Yes but wasn't Koala assigned to null?



From the book that you quoted...

Book Quoted Maxwell Xu wrote:Line 8 does the same thing. Java doesn’t care that k happens to be null. Since we are looking for a static, it doesn’t matter.



Henry
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maxwell -

Don't worry; you aren't alone. This is one of the questions, asked often by new programmers or new Java programmers.

Only one copy of a static variable exists, which is shared by all the instances of that class. When a class is loaded in memory by Java Virtual Machine (JVM), a class's static variables are also made accessible. So you don't need instances of a class to access its static members. You can use a class name to access its static variables.

Accessing static variables is confusing at times because Java allows usage of both - class name and an instance variable, to access a static variable.

For example, for the following class:


You can access the static variable, by using the class name (that is, Book) or by using the name of its instance variable (refVar):




All calls to System.out.prinltn() in the preceding code, will output 'Comic'.



If you understand the above explanation, answer these question for me:

1) Why is the 'main' method in an executable class, defined as a static method?
2) Who calls this 'main' method?

Good Luck!


With much respect,
Mala
reply
    Bookmark Topic Watch Topic
  • New Topic