can i restrict a class that it will not allows create more than two objects

Ads
 

can i restrict a class that it will not allows create more than two objects

hi all, can i restrict a class that it will not allows create more than two objects can any help me please.
Thaks bala k

View Answers

July 7, 2012 at 12:46 PM

You can restrict a class to make it's "n" number of object , please follow the below mentiond code.

public final class NnumberInstance {

private static NnumberInstance obj[];

private NnumberInstance() {

}

public static NnumberInstance[] getInstance(int n) {
    obj = new NnumberInstance[n];
    for (int i = 0; i < n; i++) {
        obj[i] = new NnumberInstance();
    }
    return obj;

}

}

public static NnumberInstance obj[];

public static void main(String[] args) {

    obj = NnumberInstance.getInstance(5);

    for (NnumberInstance instance : obj) {
        System.out.println("------------------------"+instance.hashCode());
    }

}

}


July 7, 2012 at 12:48 PM

public final class NnumberInstance {

    private static NnumberInstance obj[];

    private NnumberInstance() {

    }

    public static NnumberInstance[] getInstance(int n) {
        obj = new NnumberInstance[n];
        for (int i = 0; i < n; i++) {
            obj[i] = new NnumberInstance();
        }
        return obj;

    }
}

public class MainJavaClass {

public static NnumberInstance obj[];

    public static void main(String[] args) {

        obj = NnumberInstance.getInstance(5);

        for (NnumberInstance instance : obj) {
            System.out.println("------------------------"+instance.hashCode());
        }

    }

}

Ads









Related Tutorials/Questions & Answers:
Can a Class extend more than one Class?
Can a Class extend more than one Class?   Hi, Can a Class extend more than one Class? thanks
for writting java program why we are taking more than one class
for writting java program why we are taking more than one class  for writting java program only one class is enough but why we are taking more than one class
Advertisements
Deployment on Server that can be used simultaneously by more than one user
Deployment on Server that can be used simultaneously by more than one user  Sir, I have deployed my web application developed using JSP & sevlet. how could i access it on the network using xp os. I have deployed it on Tomcat
mapping between java class and more than 2 tables
mapping between java class and more than 2 tables  Association mapping of table in hibernate   Hi Friend, Please visit the following link: Hibernate Tutorial Thanks
there are class A & class B.How can i get members of class A& B?
there are class A & class B.How can i get members of class A& B?  there are class A & class B.How can i get members of class A& B
Can you create an object of an abstract class?
Can you create an object of an abstract class?   Hi, Can you create an object of an abstract class? Thanks
how can i create a discussion forum?
how can i create a discussion forum?  how can i create a discussion forum for my e- mentoring site for women which can be used by a registered user only. i am using jsp and servlets and i am working with netbeans 6.8.
How can I create sessionfactory in Hibernate?
How can I create sessionfactory in Hibernate?  HELLO, How can I create sessionfactory in Hibernate? If you can explain me with example that would... with the links below: Hibernate 4 Hibernate Session Factory I hope
how can I great two user and password in C++?
how can I great two user and password in C++?  how can I great two user and password in C
how can I great two user and password in C++?
how can I great two user and password in C++?  how can I great two user and password in c
how to create directed line between two buttons when i clicked there
how to create directed line between two buttons when i clicked there  Hi I have to create a topology between 6 nodes. 6 nodes are created as 6 buttons and when i clicked in first button and then on second button a directed
Can I have multiple main() methods in the same class?
Can I have multiple main() methods in the same class?  Hi, Can I have multiple main() methods in the same class?   Hi, No, We cann't... defined in the class.. For more details related to use of multiple main() method
how can retrieve more than one values in text field using ajax?
how can retrieve more than one values in text field using ajax?  im using php and ajax. Problem is that i want to search seat numbers bases of batch and semester condition on onchange event eg. if i search batch 2008 and semester
How can i pass the valus from a JSP to the action class???
How can i pass the valus from a JSP to the action class???  hewllo wevryone... can anyone help me with how i can pass the value of menuId in my JSP and pass it in the action class
I want to know how can I create sessionfactory in hibernate 4 (latest version)
I want to know how can I create sessionfactory in hibernate 4 (latest version)  Hello, I want to create sessionfactory in hibernate 4 (latest version) and I would like your help for it..An example would be of great help
how can i draw line between two panel contaning circle and line between their center?
how can i draw line between two panel contaning circle and line between their center?   how can i draw line between two panel contaning circle and line between their center
More than one Faces Configuration file
More than one Faces Configuration file  Is it possible to have more than one Faces Configuration file
create a class
create a class  plz provide any example related to class
Apply more than one xsl for single XML
Apply more than one xsl for single XML  How to apply more than one xsl for same xml file
How to create a class in java
How to create a class in java  I am a beginner in programming and tried to learn how to do programming in Java. Friends please explain how can I create a class in Java
How to Display Duplicate elements from more than one List in java?
How to Display Duplicate elements from more than one List in java?  How to Display Duplicate elements from more than one List in java? //I mean common elements from two List? Ex: List a1=new ArrayList(); a1.add(50
How to write more than 65536 rows in single excel file but more than 1 sheets?
?  Hi, i want to write more than 65536 rows in single excel file but more than 1 sheets.. now i can write only less than 65536 rows.. but in my office db having more than 1 crore data.. pls help me... here is my code.. public class
all sequences to create jdbc and how can i use jtable to display a single columns or multiple columns
all sequences to create jdbc and how can i use jtable to display a single columns or multiple columns  i am 3rd year cs student in ethiopia. i have a view in my database schema so how can i create a jtable to look the view result
Objects
writing the class definition, you can use it to create objects of that class... is an additional information about Objects that we can create multiple objects....  Real-world objects share two characteristics: They all have state
My Base Class is Changing every time in my code. How I can overcome this?
My Base Class is Changing every time in my code. How I can overcome this? ... the answer. I have a base class, 'GeneratorBaseClass' that is extended by Generator. The question I was asked was about creating a new base class
how can create pop() method in this program ,Or how can delete image in it when i press pop button in runtime ??? please help me ...
how can create pop() method in this program ,Or how can delete image in it when i press pop button in runtime ??? please help me ...   ... javax.swing.*; import java.io.*; public class AlbumInJava extends JPanel { static
problem in sending more than 500 values to a jsp page
problem in sending more than 500 values to a jsp page  when i am trying to send more than 500 values from a html form to a jsp page browser is showing that server is not sending any data...I have configured tomcat5.5
Creation of MultiThreads
;    Like creation of a single thread, You can also create more than one thread (multithreads) in a program using class Thread..., lets create the same program implenting the Runnable interface: class
Creation of Multiple Threads
;    Like creation of a single thread, You can also create more than one thread (multithreads) in a program using class Thread or implementing... create the same program implenting the Runnable interface: class MyThread1
java code to compare two hash map objects
java code to compare two hash map objects  java code to compare two hash map objects
how to handle action events in case of more than one JFrame
how to handle action events in case of more than one JFrame   I have... in it to improve the interactivity of the game, i.e i have added two #JDialog named... contains three java files namely 1. Engine.java 2. Snake.java 3. Gameoard.java when i

Ads