I just want to make clear that to call a function in two forms like
By creating an object and calling the method by using that object.
Without creating a object calling the function.
I mean for instance i have a class like
Class A{
public int callMethod(){
return 2;
}
}
Now I am creating another class to call the method callMethod defined in the Class A
Class B {
public static void main(String[] args) throws ParseException {
A a = new A();
//1st form to call the method
int aa = a.callMethod()
System.out.println(aa);
//2nd form to call the method
aa = new A().callMethod();
System.out.println(aa);
}
}
here in the first statement after creation of a object i am calling the callMethod() of class A by using the class object of A. And in the second time i am calling the method directly without creating the object class A. In the first form calling the method it is damn sure that we are creating the object and occupies some space in the memory for the object. Then what about the second form calling the method? Will it take any object creation? Which one is quicker? can anyone give me the clarifications on this.
When you use new keyword and constructor, in this case, new A(), it is creating a new object.
And in the second time i am calling the method directly without
creating the object class A.
That's not true - the object is still created, it only has no name you can refer to afterwards.
Both of your ways are creating an instance of the object but in second case you don't have variable to point to the object if you want to access the second object later you can not do it
In the second code you are creating an instance of A and it is occupying space on the stack.
The first code will require that you instantiate A on the heap space (Unless you instantiate it from static code, for example:
static {
A a = new A();
}
)
You are creating objects in the BOTH the methods. The statement
new A()
creates a new object and then you are calling a function.
You should also know that objects DO NOT get different memory space for methods. All the objects of a class share the same memory space for methods. so it really doesnt matter about the space.
If you want to call a function without using a object, you should make the function static, that way you cam call the function without actually using an object.
hope this helps.
Read about stack space and heap space in java
In java we have references referring to objects on heap space,
In both cases, it is creating object on heap space but in first case
you have reference stored on stack space with A a ("a" is reference)
So in same method or any other methos to which this reference is
passed can refer to the original object (first object created) from
heap space...
In second case you are calling methos directly on new obj which has no
reference stored on stack space to refer it afterwards like in first
case.
Both are same..!!
When you write
new A();
here Compiler calls the Non-parameterized constructor (if it is not written by programmer compiler calls the default constructor )
like:
A()
{
//i am default constructor
super();
}
note: here super(); call the immediate super-class default constructor which is the Object class and object is created.
A a = new A();
here object is created and you assign that object to reference 'a';
Related
I'm having confusion in calling a non-static method
class A {
void doThis() {}
public static void main(String... arg) {
A a1 = new A();
a1.doThis(); // method - 1
new A().doThis(); // method - 2
}
}
I know that both method-1 and method-2 will call doThis(), but is there any functional difference?
There won't be any difference in execution of those methods but in case of new A().doThis() your're going to lose the reference to the instance of an object you've invoked the method on and you won't be able to use it further in your code. All the changes this method could've done to internal state of the instance will be lost.
In case of A a1 = new A(); a1.doThis(); you're going to preserve the instance of an object (in variable a1) and potential changes made to its state made by method doThis(). Then you'll be able to continue working with this object.
Is there any functional difference?
Both will behave in the same way.
The second option doesn't allow you to reuse that instance again. It may be convenient and concise in one-line return statements (for instance, consider the builder pattern where each constructing method returns a half-initialised instance):
return new Builder().a().b().build();
or if an object was created only to perform a defined action once.
What will be the reference of a new object in method-2?
It is no longer exist (more precisely, we don't have access to it) unless the doThis returns this which you could be able to put in a variable after method execution.
Can I say that method-2 is an improper way of calling a non-static method?
No. Why should we create a variable if this variable will never be used afterwards?
Let's see what the code says in plain English:
A a1 = new A();
a1.doThis();
Create a new instance of A.
Store a reference to it in the variable a1.
Call doThis() on our instance.
Whereas new A().doThis(); reads as:
Create a new instance of A.
Call doThis() on our instance.
So the only difference is whether you store it in a local variable or not. If you don't use the value in the variable any more, then that difference doesn't matter. But if you want to call another method on the same object, let's say a1.doThat(), then you're in trouble with the second solution, as you haven't got a reference to the original instance any more.
Why would you want to use the same object? Because methods can change the internal state of the object, that's pretty much what being an object is about.
Lets take a look at both these methods one by one.
Method-1
A a1 = new A();
a1.doThis();
In method-1, you have a reference of newly created instance of A, i.e a1 and you can call as many methods on this instance of A using this reference a1. Basically you can reuse that particular instance of A by using its reference a1.
Method-2
new A().doThis();
However in method-2, you don't have any variable that stores the reference of your newly created instance of A. How will you refer to that particular instance of A if you have to call any other method on that particular instance of A ? You will not be able to re-use that instance of A if you create an instance using method-2 and you will lose that instance as soon as it is used.
case1:
A a1 = new A();
a1.doThis();
The above two line means object created and doThis(); executed but still object available in the heap memory.
case2:
new A().doThis();
A class object created and doThis(); executed after immediately GC(GarbageColletor) will activate to remove the A object from the heap memory bcz it's a non-referenced object and we can call this object as an anonymous object.
Java - I mean do instances share same instance method in memory?For example:
public class Test {
private int a;
private int b;
public Test(int a , int b){
this.a = a;
this.b = b;
}
/* instance method - sum() */
public int sum(){
return a+b;
}
public static void man(String[] args) {
Test t1 = new Test(3,4);
Test t2 = new Test(5,6);
t1.sum();
t2.sum();
}
}
I know when apply new keyword to a class, class variables (properties) will be copied. So instances can have their own property values separately.
But how about method? will it also make a copy? If so, just waste memory resources, because the methods are always same.
what happended in JVM when using a new keyword ? thanks very much!
NOTE: official doc reference is highly preferred :)
But how about method? will it also make a copy? If so, just waste
memory resources, because the methods are always same.
No, there will always be a single copy of each method (in the method area of the JVM). This applies for both static and non-static methods.
Are there some kindly people tell me what happended in JVM when using
a new keyword ? thanks very much!
Simply put, a new Object is created in the heap space by calling the appropriate constructor. A reference to that object is returned.
Methods of a class is stored on stack in .class instance which stores all methods of a class. Only one copy is created for method and it is invoked by all instance of the class.JVM keeps reference of all methods defined in a class and linked it with instance when it calls a method.
what happended in JVM when using a new keyword ?
When we simply put 'new' keyword it creates an object on the heap.
All instances share the same code.
If you compile your Java class and create an instance inside of another class of it, they will call the function on the same Java class (.class)-File.
Of course virtual calls make things somewhat more complex, but basically instance method is like a static method with additional implicit this parameter. Calling t1.sum() is like calling sum(t1) (with additional null check for t1). No reason to duplicate the method for every possible parameter value.
I have been told that the new keyword create an instance of a class and returns an object that is stored in memory and is assigned to a variable of the class type. If the statement in bold is true, why is the statement below not incorrect?
new Class();
Because there is no variable assigned to class, while new returns a variable?
Who told you that new does what you say it does? new creates an object and returns the reference to that object. It doesn't assign anything to anyone because the assignment operator in java is =
Here's an excerpt from the java tutorial:
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.
Java Tutorial
The "creates an instance of a class and returns the object address that is stored in memory" part is mostly right, except that it's technically Java's own representation of the object's location rather than the actual memory address - "reference" is a better term. The "assign it to variable" part is not related to new at all; the assignment is done by =, e.g. Class c = new Class().
If you don't have a =, the address won't be assigned to anything. This is usually pointless since it amounts to throwing the new object away, but it is legal because it is sometimes useful (if the constructor has some side effect that you're interested in). It is also legal to use the reference returned by new to call a method, e.g. new Class().doSomething(), or to pass it as a parameter: doSomethingElse(new Class()).
The above statement is not wrong because the code invoking the constructor is choosing to do nothing with the reference to the object.
Perhaps a project has an all encompassing class used to initiate view behavior. You might choose to instantiate an instance of a custom view class within the main method to mimic this behavior:
public static void main(String[] args) {
new MyClass();
}
Then in your MyClass object:
public class MyClass {
public MyClass() {
//do stuff here
}
}
new is a Java keyword. It creates a Java object for the class and object address that is stored in memory. It also allocates memory for it on the heap. new is also used for array creation, as arrays are also objects.
Example -
int[] intArray = new int[10];
String[][] stringMatrix = new String[5][10];
According to the definitions of constructors they don't have any return types,but while creating object we often do A a = new A(); which is responsible for creating the object a.
A a=new A();
Can anyone help me understanding the issue,what is actually happening in the case of constructors while creation of Object.
Constructors don't have return types, correct. But the expression new A() does have a result: A reference to the newly-created object.
Here's what happens with new A():
An object is created
It's given the type A
The relevant A constructor is called with this referring to that new object
Once initialization is done, the expression completes
The result of the expression is the reference to the new object
This process is described in this tutorial on the Oracle Java site.
In many ways, it would be more accurate to call constructors initializers: The construction happens because of the new operator, not the constructor.
The fact that constructors don't actually do the construction is made particularly clear when an object is processed by multiple constructors, as is very common. Consider:
List<String> m = new LinkedList<String>();
One object is created (ignoring any fields the list may need to initialize), but five different constructors get called to initialize that one object, because LinkedList<E> subclasses java.util.AbstractSequentialList<E> which subclasses java.util.AbstractList<E> which subclasses java.util.AbstractCollection<E> which subclasses java.lang.Object, and each of those classes has to get its chance to initialize its part of the object that was created. So in order:
JVM creates the object
Object() is called to initialize Object stuff
AbstractCollection() is called to initialize its stuff
Then AbstractList()
Then AbstractSequentialList()
Then LinkedList()
And then finally the resulting (one) object's reference becomes the result of the new expression
One object, but five constructors required to initialize it. :-)
Constructors need not to return anything. They just constructs the current instance. That's all their job, part of object creation.
Creating objects:
A a = new A();
Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
Instantiation: The new keyword is a Java operator that creates the object.
Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
Constructor declarations look like method declarations—except
that they use the name of the class and have no return type - from
the java constructor docs
To understand the constructor, it is similarly important to understand how it differs from a method.
Constructors have one purpose in life: to initialize the new object and it's fields. Nothing more. The new keyword handles the creation of memory space.
You shouldn't consider new A() to be a call to the constructor, because there are more things that happen, than just the constructor running. The major steps, when you run new A() are these.
A chunk of memory is set aside - just enough for storing an object of class A.
The constructor is run.
A reference to the chunk of memory that was set aside is returned.
So the constructor itself isn't actually returning anything - and it's an error to have return this; or anything similar inside the constructor.
Return statement inside a constructor doesn't logically makes sense because the purpose of the constructor is to perform initialization. The object has not been created yet, the actual construction of the object happens in JVM.
When does the Constructor get called?
Before object creation.
During object creation.
After object creation.
The object memory is allocated, the field variables with initial values are initialized, and then the constructor is called, but its code is executed after the constructor code of the object super class.
At the byte code level.
An object is created but not initialised.
The constructor is called, passing the object as this
The object is fully constructed/created when the constructor returns.
Note: The constructor at the byte code level includes the initial values for variables and the code in the Java constructor.
e.g.
int a = -1;
int b;
Constructor() {
super();
b = 2;
}
is the same as
int a;
int b;
Constructor() {
super();
a = -1;
b = 2;
}
Also note: the super() is always called before any part of the class is initialised.
On some JVMs you can create an object without initialising it with Unsafe.allocateInstance(). If you create the object this way, you can't call a constructor (without using JNI) but you can use reflections to initialise each field.
It gets called at object creation. The memory must be reserved first for the object, otherwise the constructor code could not run. So maybe we could say after object creation. Also note that initialization code written in the class gets called before the constructor code.
public Ex {
int xVal = -1;
int yVal;
public Ex() {
// xVal is already -1.
//yVal defaults to 0.
}
}
THE JVM will first allocate the memory for your object, then initialize all fields, then invoke your constructor.
The constructor gets called when a new object is created.
NewObject n = new NewObject();
public class NewObject {
public NewObject() {
// do stuff when object created
}
}
Hope this helps.
basically constructors are called to initialize the values of the instance variables except the case for default constructors. However, this initialization of the instance variables are done in 4 steps (as applicable):
variables are initialized with default values (ints with 0, chars with u\0000 etc.)
variables are initialized with explicit initialization values
initialized with static blocks
constructors are called
After the Object creation
once an object is created using new operator like Student s = new Student();
first Student object is created, and then constructor is called to initialize the variable of the object
We can prove constructor is called after creating the object by using below code
here we are using instance block. And also instance block is executed before the constructor
so I am printing hash code in three places
Inside Instance block
Inside Constructor
Inside main mehtod
all these three times hash code is equal, that means object is created before the constructor is executed
because having a hash code means, there must be an object. And if hash code printed inside both instance block and constructor is equal. that means object must be created before the constructor execution
Constructor is what practically creates object. It is called when object is created by executing new MyClass() (or its parametrized version).
Given those options, 1. Before object creation.
After the constructor finishes, the object has been created.
Whenever we create an object by using 'new' operator then 1st task is performed by the new i.e. it allocates the memory for object in heap with pointing to the reference variable in stack and set the initial values of object fields.then it calls the constructor with passing 'this' as object to initialize it according to your requirement...
So the constructor is always called after the object creation....
Note: When you enter in constructor so 'this' keyword is working means your object has been created.