Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Static variable or method  RSS feed

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have this prgram of OCA book

when i run this code it print twice 0  but i don't undrestand why after affect the object k to null the value of count still printed 0 but when i delete  static  before count  and rexecute this program it print first 0 and then i  print an exception
Exception in thread "main" java.lang.NullPointerException
at pkgB.koala.main(test1.java:7)
 
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us full details of where code or quotes come from; in this case, author's name(s) and page number. That reduces copyright problems and makes it easy to review the rest of the source. Only I probably don't have that book anyway.

I think it was a mistake in the language, but you are allowed to call static methods like this myObjectReference.someStaticMethod(). I think they should have restricted use of static members to ClassName.someStaticMethod(). But they didn't. You are referencing that static field from the type of the reference. The compiler thinks in line 5 that k is of type Koala, so it converts the call to Koala.count. When you get to line 7, it still thinks the type of the reference is Koala, so it still converts your call to Koala.count. You can even do something like this:- ((Arrays)null).sort(myArray); and you will get a sorted array and no exceptions. But it doesn't mean it's anything ike good programming. You can also get problems with trying to use a static method polymorphically. As you know, static methods are never polymorphic.If there is any difference between the static method in Car and that in Taxi, you get the Car's static method because its binding is static; it is fixed at compile‑time and doesn't change. The binding is static to the type, maybe that is why they chose the keyword static in the first place. Bound statically to Car, so you don't get the Taxi static method.
In line 4, the binding is dynamic and is done at runtime, so you get polymorphism, but that only applies to instance methods.
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was the bad indentation in the code caused by your copying or was it that badly indented in the book? Remember that people who write cert exam practice questions often indent the code badly so as to make the question harder for learners to answer.
Whatever I said about access to static methods applies to static fields too when the usual rules about private and public are applied.
 
Popeye has his spinach. I have this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!