• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Static variable or method

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 95
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

emma roberts wrote:i have this prgram of OCA book
[code=java]
public class koala{
public static int count=0;
public static void main(String[] args){
koala k=new koala();
System.out.println(k.count);
k=null;
System.out.println(k.count);

}

}

K=null means object is null but not the reference, so is still there you can refer to static variable with reference 'k' and if you remove static keyword then it becomes object variable so as long as object avaliable field is available, but you kept the object null, last print statement gives null pointer exception because object is null so there is no field so error

 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sohail hussain wrote:. . . K=null means object is null but not the reference,  . . .

Actually, it is the other way round. The reference points to null, and we don't know anything about any objects from that reference. You are right that you can call a static method on null, however. As I said neore, you can write dreadful code like ((Arrays)null).sort(myArray); and it will compile and will even run as long as the array's elements are primitives or implement Comparable<T>.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic