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

Class instantiation

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I use objects methods by only having a reference, without actually creating the object?

For example, GeneralPath is a class in java.awt.geom package.

Can I say


Here if the point evaluates to null, then I will go on creating a new GeneralPath object.



sombody clear me...
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ravi:

Yes you can use methods by only having a reference.

Your methods must be declared as static.

And you don�t have to instance the Class.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For this code,

,

you will get a NullPointerException.

You cannot call a method on a reference which is null.

getCurrentPoint() doesn't have any meaning if gPath isn't pointing to a GeneralPath object.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surprisingly (or maybe not) you can use static members of a null variable.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed you can get static members from a class whose instances are all null, but are you really "using static members of a null variable," Stan James? The static references are to the class, and are available whether you have 0, 1 or 10000000 instances or null declarations of the class. In fact your String HELLO is loaded before the main method and before the "t" variable is declared.
 
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
Ofcourse you're not using "static members of a null variable". Static members are at the class level, not at the instance level. A static member does not belong to any specific instance, but to a class.

In my opinion, it should have been illegal in Java to call static methods on an instance (or on a reference variable, whether it is null or not). Calling static members on an instance is confusing and I don't know any reason why it would be necessary to have this as a feature in the language.

We often have questions in the forums from people who don't understand that static methods are not polymorphic. One of the reasons people get confused about this is because it's possible to call static methods on instances.

Always call static methods on the class, not on an instance:

[ April 18, 2007: Message edited by: Jesper Young ]
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and if Jesper Young had his wish and calling static methods on instances were prohibited (rather than simply raising a warning), we woul dhave a nice simple answer obvious to all who ask "can you call methods of a null reference?"


It would be NO.

And if by object's methods you mean methods which actually belong to that object, then it is still impossible to call them on a null reference.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"using static members of a null variable"



We'd have to agree on what that really means. The source code uses a variable t which is null, so I think I'm allowed to read it that way. Of course the compiler doesn't read it that way. It binds the method to the class and goes on its way. But if I tried to express every concept the way the compiler sees it, I'd have to be the compiler. Not a job I want.
reply
    Bookmark Topic Watch Topic
  • New Topic