answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

βˆ™ 14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you limit object creation in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When constructor function are called in java?

Constructors are called during object creation.


What is a java object?

A java object is a collection of methods and properties defined in the Java programming language.


What is object in java programming?

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.


What language is completely object oriented c plus plus or java?

Java is the complete object oriented Programming Language as every thing in java is an object,


Can you initialize an object without constructor in java?

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.

Related questions

When constructor function are called in java?

Constructors are called during object creation.


What is use of constructor in java?

Constructor is used to do something (written in constructor) immediately after object creation.


What is a java object?

A java object is a collection of methods and properties defined in the Java programming language.


What is object in java programming?

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.


What language is completely object oriented c plus plus or java?

Java is the complete object oriented Programming Language as every thing in java is an object,


How do we declare objects?

In Java, write the name of a class, followed by a variable name. For example:String myName;This won't actually create the object; you also need to create the object:myName = new("Hi, it's me.");These two parts (declaration, and object creation) can be combined:String myName = new String("Hi, it's me.");(In the case of a String, in Java you can just assign a quoted string, instead of using the new operator. However, I wanted to show the general syntax for object creation, that works for other kinds of objects too.)In Java, write the name of a class, followed by a variable name. For example:String myName;This won't actually create the object; you also need to create the object:myName = new("Hi, it's me.");These two parts (declaration, and object creation) can be combined:String myName = new String("Hi, it's me.");(In the case of a String, in Java you can just assign a quoted string, instead of using the new operator. However, I wanted to show the general syntax for object creation, that works for other kinds of objects too.)In Java, write the name of a class, followed by a variable name. For example:String myName;This won't actually create the object; you also need to create the object:myName = new("Hi, it's me.");These two parts (declaration, and object creation) can be combined:String myName = new String("Hi, it's me.");(In the case of a String, in Java you can just assign a quoted string, instead of using the new operator. However, I wanted to show the general syntax for object creation, that works for other kinds of objects too.)In Java, write the name of a class, followed by a variable name. For example:String myName;This won't actually create the object; you also need to create the object:myName = new("Hi, it's me.");These two parts (declaration, and object creation) can be combined:String myName = new String("Hi, it's me.");(In the case of a String, in Java you can just assign a quoted string, instead of using the new operator. However, I wanted to show the general syntax for object creation, that works for other kinds of objects too.)


Can you initialize an object without constructor in java?

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.


Why java is certifiction?

java is a object oriented langage,that is indepentant platform. java is full object oriented language. java is access any operating system.so java is good certification of program in runtime environment.


Did java oriented language works on object?

Java is an object oriented language, and it works with classes and objects.


Is there class name as object in java?

Yes, the base class for all other Java classes is Object.


Why do you require a object in java but not in C?

Because Java is an object-oriented language and C is a procedural language.


What is the Object class parent?

Object is the topmost class in the Java Class hierarchy. There is no Class above Object. All classes in Java are implicitly derived from Object.