public class Employee {
	
	private String name;
	private String SSN;
	private Employee e = new Employee("ME","MYSSN");
	Driver x = new Driver(32);
	public Employee(){
		
	}
	public Employee(Employee e){
		this(e.name,e.SSN);
		
	}
	
	public Employee(String name, String SSN){
		this.name = name;
		this.SSN = SSN;
	}

	
	
	public static void main(String [] args){
		Employee e = new Employee("beletech", "me beletech");
		try {
			Object f = e.clone();
		} catch (CloneNotSupportedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
				
		
	}
}

Can anyone help? I'm getting a stack overflow error....the Driver class I have used in here is just a class that has one public member, licencenum .......

One more thing..i'm not sure how to put the tags around the code..help on that one too :(:(:((:(
thanks loads everyone

This code: private Employee e = new Employee("ME","MYSSN"); calls the constructor.

public class Employee {
	
[B]	private Employee e = new Employee("ME","MYSSN");[/B]
	
	public Employee(Employee e){
		this(e.name,e.SSN);
		
	}

When the constructor of a class is called you create an instance of that class and its attributes are declared and/or initialized.
Meaning that when you do this: private Employee e = new Employee("ME","MYSSN"); you call the constructor that creates a new instance. When you do that the e attribute of the class is created and it calls this: private Employee e = new Employee("ME","MYSSN"); . When you do that the e attribute of the class is created and it calls this: private Employee e = new Employee("ME","MYSSN"); When you do that the e attribute of the class is created and it calls this: private Employee e = new Employee("ME","MYSSN"); When you do that the e attribute of the class is created and it calls this: private Employee e = new Employee("ME","MYSSN"); When you do that the e attribute of the class is created and it calls this: private Employee e = new Employee("ME","MYSSN"); So after a while when you create an instance you call this: ( private Employee e = new Employee("ME","MYSSN"); ) That piece of code creates another instance that calls the private Employee e = new Employee("ME","MYSSN"); which creates another instance, ...

So remove the private Employee e = new Employee("ME","MYSSN"); from the code

my badd...thanks lots..i just kept on focusing on the clone..:):):) solved!!

my badd...thanks lots..i just kept on focusing on the clone..:):):) solved!!

If you want to have an attribute that is the instance of the class, if you make it static, it will work. But do that only when it is useful. If you can't think of why, then you shouldn't but this is how it is done:

public class Main {
    [B]static[/B] Main main2 = new Main();

    public Main() {
        System.out.println("a");
    }

    public static void main(String [] args) throws Exception {
        Main main = new Main();
    }
}

Try running the above and see what happens and then remove the static from the second line.

hmm..interestesing...i like the effect..i even commented the line inside main..and i got only one 'a' printed. just to be clear......correct me if i'm wrong.. i am thinking this is happening because the the static reference main2 to the object is executed before the programm startes from main....and is not stored as a member of the class object if main?...
just curious..

hmm..interestesing...i like the effect..i even commented the line inside main..and i got only one 'a' printed. just to be clear......correct me if i'm wrong.. i am thinking this is happening because the the static reference main2 to the object is executed before the programm startes from main....and is not stored as a member of the class object if main?...
just curious..

If it is not static, then this: Main main2 = new Main(); would be called any time you create a new instance, which also creates a new instance and so on and so on.

static fields don't need an instance of the class in order to be accessed. So when this is called: static Main main2 = new Main(); the constructor is called and a new instance is created and all the attributes are created but not the static fields because they are not part of the instance itself.

I don't know a better way to explain it the technical terms elude me

..i think i get the idea...thanks loads..:):)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.