• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

avoid using objects to access a class variable. why?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I came across this coding standard.

Avoid using an object to access a class (static) variable or method. Use a class name instead. For example:


I would like to know the reason as why objects should not be used?
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clarity. It makes it quite clear that you are referencing a class member as opposed to an instance member.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class methods (static methods) are methods that don't work on an object, but that are for the whole class.

When you call a class method on an instance, the code looks as if it is a regular non-static method that works on the object that you call it on. But that's not really the case, so code written in that way will look confusing.

In my opinion, it should not have been allowed at all to call static methods on instances - I regard it as a mistake in the design of the Java programming language that this is possible.
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:In my opinion, it should not have been allowed at all to call static methods on instances



I agree that it will be less confusing if class methods can only be accessed via classes, however, I'm sure that many people will raise an eyebrow if an instance is not allowed to directly access or modify the state of its own class.
 
Marshal
Posts: 80294
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Restricting invocation of static members to className.member would not prevent an instance from accessing static members of its own class.

But because of hiding, the member you are accessing via an instance might be different from that accessed via the class name.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm talking about restricting invocation of static members to className.staticMember and banning instanceName.staticMember. This will be less confusing, but many people will raise an eyebrow if an instance is not allowed to directly access or modify the state of its own class. One way around this is an instance method that can access or modify the state of its own class, however this method should be a static method.
 
Fire me boy! Cool, soothing, shameless self promotion:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic