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?

Continue Learning about Engineering

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.


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 the function of the word 'private' in class variable declaration?

In class variable declaration, the word 'private' serves as an access modifier that restricts visibility and accessibility of the variable to the class in which it is declared. This means that the variable cannot be accessed directly from outside the class, promoting encapsulation and protecting the internal state of the class. By using 'private', developers can control how the variable is accessed and modified, typically through public getter and setter methods.


What are Instance variables java?

Instance variableA variable, part of an Object. These might better be called perObject variables since each instantiated object of this class will have its own private copy of this variable. They are allocated when the object is allocated via new. Static methods may not access the instance variables of their class (or any other class for that matter), other that via some object reference, e.g. anObject.someField. Static methods may even access private instance variables in their class via some object reference.


Is the part of the program in which a variable may be accessed?

Your class, enum or getter methods.

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"


What is the function of the word 'private' in class variable declaration?

In class variable declaration, the word 'private' serves as an access modifier that restricts visibility and accessibility of the variable to the class in which it is declared. This means that the variable cannot be accessed directly from outside the class, promoting encapsulation and protecting the internal state of the class. By using 'private', developers can control how the variable is accessed and modified, typically through public getter and setter methods.


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.