Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

refrence variable of object

 
sindura kapur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic