Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to assign value to integer x  RSS feed

 
Ranch Hand
Posts: 139
  • 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 ]
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • 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.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, constructors do not return values.
 
Sheriff
Posts: 7023
  • 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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am greatly thankful for all your input.
thanks
siva
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!