This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to assign value to integer x

 
Siva kandasamy
Ranch Hand
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Blough
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, constructors do not return values.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...but it is allowed to have methods with the exact same name as the constructor.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am greatly thankful for all your input.
thanks
siva
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic