• Post Reply Bookmark Topic Watch Topic
  • New Topic

class instantiate  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we instantiate this class?

public class A
{
A a = new A();
}
 
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't instantiate class we instantiate an instance of a class i.e an object of a class. Here we can compile it successfully but can't instantiate because at run time JVM searches for main method which is missing here so Run time Error: main method not found in class A
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried making a new instance of A?
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created two classes A.java



and PSVMain.java



There are no compile time errors. Running it in NetBeans IDE gave

Exception in thread "main" java.lang.StackOverflowError
at com.javaranch.mahesh.testclassinstantiation.A.<init>(A.java:10)
at com.javaranch.mahesh.testclassinstantiation.A.<init>(A.java:10)
...<many more times>...

 
Sheriff
Posts: 4930
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could go through Oracle's tutorial about Objects (<- link)
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you can instantiate that class. You have done that. Now you need to work out, probably with pencil and paper, what happens when you instantiate that class.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apology,
The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.
Ref: JLS
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you worked out why you are getting the stack overflow?
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have you worked out why you are getting the stack overflow?


In line 5 of PSVMain.java

A a = new A();

leads to creation of a new instance of A (lets call it instance 1) by calling constructor of A(). The class A.java in line 4 creates a new instance of A (lets call it instance 2) to which it holds a reference in instance variable a. When creating an new instance (what we called instance 2) in line 4 of A.java, the newly created instance 2 creates another new instance of A by calling constructor of A() which is instance 3 and so on... Thus when an instance "n" of A is created it will create a new instance "n+1" of A calling the constructor of A(). This leads to infinite recursion, that's the reason for java.lang.StackOverflowError. This is like a recursive function which does not properly return, only here its a constructor call which keeps on infinitely calling itself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!