• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple String assignment  RSS feed

 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this compiles:
class Car
{
String color = "blue";
}
this does not compile:
class Car
{
String color;
color = "blue";
}
2 errors: "identifier expected" pointing at the "="
and "cannot resolve symbol" pointing at color.
This doesn'take sense...
 
Pat Barrett
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Herb,
Try putting your #2 code in public static void main block a-la....

and it will work fine. I believe that the reason for this has something to do with how you are declaring your string. I'm sorry, but I'm a bit vague in the specifics.
Pat B.
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thankyou for the feedback, but I know how to get it to compile. I just wanted to know why it will not compile in the second case. In both cases there is an assignment which I thought should be identical in effect. But it is not...
 
Randall Twede
Ranch Hand
Posts: 4692
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare and assign at the same time or you can just declare. But to just assign it must be in a method or constructor.
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I see now this general rule also applies to other Objects and primitives as well besides the String class. In a way the rule makes some sense since we don't want Object assignments going on willy-nilly anywhere do we ? (except for initialization). Thanks for THE ANSWER, I can sleep now tonight. I thought knew Strings cold...
 
Banaja Bhaduri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randal, Thanks for your reply. But as per my understading it's not a problem of declaration and assignment. If we declare and assign at the same time like following code, we get a compiler error pointing at i in System.out.println(i) method, 'identifier expected'. When i has been declared and initialised at the same time. If you can please explain this.
class test
{int i = 7;
int j = 10;
System.out.println(i);

public static void main(String arg[])
{int i = 10;
int j = 7;
System.out.println(i+j);
test basis = new test();
System.out.println(basis.i);
}
}
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Banaja,
I am not sure why System.out.println() gives these two specific errors... Even if you take out the 'i' in your System.out.println() and leave it blank (to just produce a newline, or replace it with a static like Math.PI, it still gives the two errors :

In any case, the only way this will work is to put the System.out.println() in a static block and have it print a static variable. The "rule" Randall mentioned above doesn't just pertain to assignment operations... any statement except a declaration (or a declaration and an assignment as one statement) cannot be in a class outside of a method or a static block.
Even if this somehow worked, even if you declare and initialize a variable at the same time, System.out.println(i) would not work the way you did it... i is a member variable, and thus would not exist until an instance of the class that contained it existed... (if i was static, perhaps you could... if Java let you run the System.out.println() method anywhere in a class...) This is kinda like how static methods can only access static variables...
Anyway, HTH,
-Nate
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding a new dimension to the discussion this compiles fine

------------------
Regds.
Mahindrakar
 
Banaja Bhaduri
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Nathan, now I got it.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul,
Wow! That sent me to the JLS to see why it actually ran... I never knew about instance initializers! Guess you learn something new about Java every day! (At least I do... )
OK... change the "rule" to read... "Nothing can be inside a class and outside a method, except a declaration (or a declaration and assignment as one statement), inner classes (forgot this one too...), static initializers, or instance initializers." Now (hopefully) this statement is correct...
Thanks,
-Nate
[This message has been edited by Nathan Pruett (edited February 14, 2001).]
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nathan.
As an intermediate beginner, finding these so very simple, so very basic and fundamental things not mentioned in the books (not the 5 I checked)is frustrating. Without these forums many people would get too discouraged about Java.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!