Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

need explanation for this sentence

 
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.
 
Marshal
Posts: 25817
69
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?
 
Ranch Hand
Posts: 82
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.


 
Saloon Keeper
Posts: 12254
259
  • 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.
 
Marshal
Posts: 70263
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Yes, that is horrible code, isn't it.
 
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.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.  
 
Alas, poor Yorick, he knew this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic