File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Junior/Associate Certification (SCJA,OCAJ 5/6/8) and the fly likes refrence  variable of object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Junior/Associate Certification (SCJA,OCAJ 5/6/8)
Bookmark "refrence  variable of object" Watch "refrence  variable of object" New topic

refrence variable of object

sindura kapur

Joined: Jul 14, 2008
Posts: 22
can an object refrence variable be used to call a method like:
object a;

instead of
object a=new object();
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

You still need to create the instance, especially if the method you are calling is non static, as it would be with the Object class.

The toString() method is a real method in the Object class.

Your first statement is probably equivalent to:

This is valid. But it could be also done in one step, but you need to ensure the object is created with the new keyword:

That pretty much does the equivalent of the first code segment, but in one line. I'm not sure if it looks as good or is as readable, but it's valid.

With static methods and properties, you don't need an instance, so you can just do something like this:

-Cameron McKenzie
[ July 20, 2008: Message edited by: Cameron Wallace McKenzie ]
I agree. Here's the link:
subject: refrence variable of object
It's not a secret anymore!