• Post Reply Bookmark Topic Watch Topic
  • New Topic

need explanation for this sentence  RSS feed

 
Jyothirmayi Pendala
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A null reference may be used to access a static variable or method.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us where you read that sentence?
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jyothirmayi Pendala wrote:A null reference may be used to access a static variable or method.


For example:



null reference of Person:

the output:


It means that null reference of Person can access static variable count in Person class.


 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, it's allowed to refer to static members through an object reference, instead of using the class name. Since static members don't require an object to be accessed, null is a valid option.

Accessing static members through object references is very poor style though. I don't know why the language designers chose to allow it.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Yes, that is horrible code, isn't it.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks horrible indeed. So many horrible things are possible in Java, for example
It's up to the programmer to adopt best coding practices.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Cox wrote:It looks horrible indeed. So many horrible things are possible in Java, ...

Yes, but that does not make Java a bad language. You can also do horrible things with a hammer, but that doesn't make a hammer a bad tool that you shouldn't use.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm definitely not saying that Java is a bad language. I'm simply emphasizing the need to adopt best coding practices. Otherwise, horrible things can happen. 
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!