wood burning stoves 2.0*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes refrence  variable of object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "refrence  variable of object" Watch "refrence  variable of object" New topic
Author

refrence variable of object

sindura kapur
Greenhorn

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

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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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: http://aspose.com/file-tools
 
subject: refrence variable of object
 
Similar Threads
BufferedReader
Parent / Child relationship problem
whats the difference between Objects and reference variables?
== operator
confuse in {Parent p = new Child();}