| Author |
Unable to assign value to integer x
|
Siva kandasamy
Ranch Hand
Joined: Dec 31, 2002
Posts: 139
|
|
Hi There, See the code below. Do you see any reason, why I am unable to assign value to integer x using function. Code: // Tom.java class Tag { Tag(){ System.out.println("tag"); } Tag(int x ){ System.out.println("tag-arg"); } int Tag(){ System.out.println("tag-func"); return (int) 100; } } class Tim { int x, y; Tag t1 = new Tag(); Tag t2 = new Tag(10); //x=t1.Tag(); x = Tim1(); int Tim1() { return 101; } } public class Tom { public static void main(String[] args) { Tom d = new Tom(); } } ///:~ [ October 16, 2003: Message edited by: Sivanantham kandan ]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
Both of these lines in the class "Tim" are illegal Java because they're at class scope. Assignment statements like these are only allowed inside methods or inside static or instance initialization blocks. A line like is legal, though, because it's not a statement, it's a declaration with an initializer, and those are allowed. You could move the offending lines inside the "Tim" class's constructor.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Tom Blough
Ranch Hand
Joined: Jul 31, 2003
Posts: 263
|
|
Also, constructors do not return values.
|
Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
|
...but it is allowed to have methods with the exact same name as the constructor.
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
So if the original poster is still listening: this is an example of why naming conventions are a Good Thing. It's pretty much universally accepted that Java class names LookLikeThis, while method names lookLikeThis (note initial lower case letter.) [ October 17, 2003: Message edited by: Ernest Friedman-Hill ]
|
 |
Siva kandasamy
Ranch Hand
Joined: Dec 31, 2002
Posts: 139
|
|
I am greatly thankful for all your input. thanks siva
|
 |
 |
|
|
subject: Unable to assign value to integer x
|
|
|