aspose file tools*
The moose likes Beginning Java and the fly likes constructor problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor problem" Watch "constructor problem" New topic
Author

constructor problem

Samarth Barthwal
Greenhorn

Joined: Feb 16, 2006
Posts: 23
In the following code when I take the name of the reference same while calling both the constructors i get an error stating "c is already defined in main(java.lang.String[])".But when I change the name of one of the references I get no errors.Why is it so?


marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A type (cont) followed by a variable name (c) is the syntax for declaring a variable. So when you say "cont c" a second time, Java thinks you're trying to declare a second variable with the same name as the first.

If you want to use the same c after declaring it, just use the variable name without the type...

cont c = new cont(); //declaration (and assignment)
c = new cont(6); //reassignment

If you want a different variable, then provide the type for the new variable...

cont c = new cont();
cont d = new cont(6);


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Samarth Barthwal
Greenhorn

Joined: Feb 16, 2006
Posts: 23
I am still confused
What I have learned is that on writing:

we create a memory space for an object of cont type,and when we write:

we create a reference to the memory allocated to the object.
Then when we write:

What is actually happening and why?
[ March 20, 2006: Message edited by: Samarth Barthwal ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

When a variable is first declared, its type needs to be specified. But any time after the declaration, the variable name is used by itself (without the type). For example...

cont c; //variable declaration
c = new cont(); //assignment

Note that in the above example, the second line would not be valid unless we had previously declared 'c' as a variable of type cont.

Declaration and assignment can be combined in the same line...

cont c = new cont(); //declaration AND assignment
Samarth Barthwal
Greenhorn

Joined: Feb 16, 2006
Posts: 23
Thanks Marc for the help.
[ March 21, 2006: Message edited by: Samarth Barthwal ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor problem
 
Similar Threads
I need Help With an Interface...plz
! CONFUSED in array name
Problem instantiating an overloaded constructor using Reflections ...
Doubt on thread sleep method
Cursor in JTextPane