answersLogoWhite

0

Creating an object and invoking a method of that object are 2 distinct steps. From good programming practice, it is better off to have 2 separate statements at least to do this.

For example:

(new object()).ToString(); // creates an instance and invoke ToString()

vs.

object objRefence = new object();

objRefence.ToString();

The variable objReference is needed to invoke the method ToString(), because the object refereced of the one being created from the new operator needed to be cached somewhere.

There is no major differences between the 2 above. However, if there is another method to be invoked, the second way (use variable reference) is the only way, the first one cannot. (You can replace ToString() with GetHashCode(), and prints it on the console twice, the first one may produce 2 different values, while the second one will be the same value. Think of GetHashCode() returns the object id in a way)

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

Can variables declared inside of methods be accessible to other methods in the same class?

No. Not directly, unless you pass a reference to that variable when you call a method.


How do you access the static variable and static methods of a class?

In java we access static variables and static methods without creating objects. i.e.,we can access directly by using classname we can also access static variables and static methods by using objects which are created by using class where the static variables and static methods are available


Java Which access modifier is preferred for instance variables?

Usually Private is the preferred access modifier for instance variables. Benefits: 1. No other class can access this variable directly. They can do only through the getter/setter methods 2. Only the methods in that particular class can use this variable


What is a polymorphism in java programming?

Polymorphism can be considered as the ability of one thing being multiple other things (though partially or fully). Am I confusing you? I believe yes. To put it in simpler words, any java object that can pass more than one Is-A test can be considered polymorphic. Other than objects of type Object, all Java objects are polymorphic in that they pass the IS-A test for their own type and for class Object. Remember that the only way to access an object is through a reference variable, and there are a few key things to remember about references: • A reference variable can be of only one type, and once declared, that type can never be changed (although the object it references can change). • A reference is a variable, so it can be reassigned to other objects, (unless the reference is declared final). • A reference variable's type determines the methods that can be invoked on the object the variable is referencing. • A reference variable can refer to any object of the same type as the declared reference, or-this is the big one-it can refer to any subtype of the declared type! • A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface.


What is polymorphism and types of polymorphism?

Polymorphism can be considered as the ability of one thing being multiple other things (though partially or fully). Am I confusing you? I believe yes. To put it in simpler words, any java object that can pass more than one Is-A test can be considered polymorphic. Other than objects of type Object, all Java objects are polymorphic in that they pass the IS-A test for their own type and for class Object. Remember that the only way to access an object is through a reference variable, and there are a few key things to remember about references: • A reference variable can be of only one type, and once declared, that type can never be changed (although the object it references can change). • A reference is a variable, so it can be reassigned to other objects, (unless the reference is declared final). • A reference variable's type determines the methods that can be invoked on the object the variable is referencing. • A reference variable can refer to any object of the same type as the declared reference, or-this is the big one-it can refer to any subtype of the declared type! • A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface. The two important types of Polymorphism are: • Method Overloading and • Method Overriding


Varience methods by which you would split semi-variable costs in its fixed and variable elements?

assignment


What methods is most helpful for revealing cause-effect relationships?

Experiments with a control and variable...not correlational studies because they don't ensure that the cause led directly to the "effects"


To solve a three variable system of equations you can use a combination of the elimination and substitution methods?

True. To solve a three variable system of equations you can use a combination of the elimination and substitution methods.


What is casting and its type?

Simply stated, casting allows you to change the type (class) of an object. Technically, the object itself does not change type, but your reference is changed. For example, lets suppose you have the class "Animal" with subclasses "Cat" and "Dog". If you have a reference (variable) declared to be type "Animal" and you store and object of type "Dog" in it, you will not be able to call any methods from the "Dog" class which the "Animal" class does not have. You would first need to cast your reference to "Dog" so that you can utilize those methods.


What is object in methods in java?

I guess an object in a method would be any object variable, used within a method. An object variable of course is a variable that is declared as a variable of a certain class type.


What is an example of a hidden variable in psychology apex?

An example of a hidden variable in psychology is individual differences in personality traits. These traits are not directly observable but can have a significant impact on behavior, thoughts, and emotions. Researchers often use self-report measures or observational methods to indirectly assess these hidden variables.


What is the code required to convert an integer variable to a string variable in Java?

There are several different methods to convert an integer variable to a string variable in Java. For example, one can use the following code to convert an integer variable to a string variable: Integer.toString(number)