Hi, the answer for this question is as follows....:
class Demo
{
public static void main(String[] ar) throws Exception
{
Test1 d = new Test1();
Test1 d1 = new Test1();
boolean b = d1.equals(d);
Test1 d2 = new Test1();
System.out.println("" + b);
}
}
class Test1
{
static int count=0;
public Test1() throws Exception
{
try
{
if(count > 1)
{
throw new Exception();
}
else
System.out.println("Hi");
count=count + 1;
}
catch(Exception e){System.out.println("Can't have more than two instances");}
}
}
regards,
Amit
Chat with our AI personalities
Constructors are called during object creation.
A java object is a collection of methods and properties defined in the Java programming language.
In java object is an instance of a class. Objects are created using the new keyword. When you use the new keyword along with a class name, an object of that class would get created. Ex: Ferrari obj = new Ferrari(); Here a new object of Ferrari gets created. A constructor of the class Ferrari would get invoked during the object creation.
Java is the complete object oriented Programming Language as every thing in java is an object,
No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.