File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Class instantiation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class instantiation" Watch "Class instantiation" New topic

Class instantiation

Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
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

Joined: Jan 17, 2007
Posts: 30
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.

Greetings<br />Nacho Espinosa<br />SCJP 1.4
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
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

Joined: Jan 29, 2003
Posts: 8791
Surprisingly (or maybe not) you can use static members of a null variable.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
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

Joined: Aug 16, 2005
Posts: 15081

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
. . . 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

Joined: Jan 29, 2003
Posts: 8791
"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.
I agree. Here's the link:
subject: Class instantiation
It's not a secret anymore!