• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class instantiation

 
Ravi Kotha
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • 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...
 
Nacho Espinosa
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surprisingly (or maybe not) you can use static members of a null variable.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • 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)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic