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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Local variable" Watch "Local variable" New topic
Author

Local variable

Ashish Paliwal
Greenhorn

Joined: Oct 07, 2009
Posts: 9
Class A {
public static void main(String args[])
{
String s = "ashish";
t = s.length();
while(t.length > 3)
int i;
}
}

I know this code is not doing anything useful. But my doubt is if I declare any variable inside while loop like this one, then it gives me 4 errors in that line. Doesnt matter which refrence variables you are using int or List.
More strangely, if I put { } in this one statement while loop then it works.
I a absolutely puzzled ??

I am sorry if this sounds too simple for people but I am newbie.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
what are the errors you are getting? Have you declared 't' somewhere?
Ashish Paliwal
Greenhorn

Joined: Oct 07, 2009
Posts: 9
this is the only code. Only variable declared in whole program is t.

Errors are :

1) '.class" expected
2) not a statement
3) illegal start of expression
4) ; expected

all the errors are in that line only "int t;"

Can someone please try this small program and let me know if this is working. As I told, strangely enough if I put brackets in this one line while loop, it works.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Please use code tags and post real code. With the code you gave us (which you probably retyped) I see so many more problems:
1) Class starting with a capital C
2) t is never declared
3) t is obviously meant to be an int yet you try to call its length field like it is an array

So please copy/paste the real code and put them in code tags to preserve the formatting.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
ohh I see. length is not a method in String class. please read more about String's length and Array's length()
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

There are a number of things wrong with the code.

First, let's put it between code tags, so that it's easier to read and line numbers are added automatically (note, please use code tags yourself next time when you post code):

Problems:

  • Line 1: It should be "class" with a lower-case "c", not "Class" with an upper-case "C".
  • Line 5: You didn't declare the variable t. You probably meant: int t = s.length();
  • Line 6: If t is an int, then it doesn't have a property called "length".
  • Line 7: Why are you declaring an unused variable i here? Did you mean to put more code in the while loop?


  • Rahul.p Kumar wrote:ohh I see. length is not a method in String class. please read more about String's length and Array's length()

    There is a method called length() in class String. Look it up in the API documentation.


    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    Ashish Paliwal
    Greenhorn

    Joined: Oct 07, 2009
    Posts: 9
    Doesn't matter. Try with length() or with while(true) or with any condition....
    it is not working
    Ashish Paliwal
    Greenhorn

    Joined: Oct 07, 2009
    Posts: 9
    I am sorry for undeclared t and capital C. I just wrote rather than copy pasting from IDE.

    My point is :

    even this

    while(true)
    int i;

    is showing errors. And if I put braces around while loop, everything is working. Say above 2 lines are only thing inside main method. Then also it is not working.
    Ashish Paliwal
    Greenhorn

    Joined: Oct 07, 2009
    Posts: 9
    Here is the actual code

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39409
        
      28
    You are not allowed plain declarations like int i; inside a loop. At least I think so; that is one of your compiling problems.
    Ashish Paliwal
    Greenhorn

    Joined: Oct 07, 2009
    Posts: 9
    Ok, then why does it work after putting braces around while loop.
    Also, its not about int i, declare any variable, List l or Object o;

    It will give errors.

    I am not able to figure put what's the problem
    Ashish Paliwal
    Greenhorn

    Joined: Oct 07, 2009
    Posts: 9
    Ok, probably I figured out the problem.
    There is no block scope for that variable. Variables are always declared in some blocks. So that is why it was working with braces.
    It happens same way in C also.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39409
        
      28
    I don't think you have figured it out. No, it doesn't work the same way in C at all.
    If you have previously written C you need to forget it; the rules about scope are completely different.
    No, it is not a scoping problem; it is that loops have to contain statements (at least I think they do; you would have to check the Java™ Language Specification for the full details of the grammar). But int i; is not a statement, but a declaration. So it is not permitted inside a loop.

    At least I think that's the problem; when I tried your code I got a "not a statement" error for that line.
    Ashish Paliwal
    Greenhorn

    Joined: Oct 07, 2009
    Posts: 9
    hmmm probably right.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19720
        
      20

    Ashish Paliwal wrote:Ok, then why does it work after putting braces around while loop.
    Also, its not about int i, declare any variable, List l or Object o;

    It will give errors.

    I am not able to figure put what's the problem

    The body of statements like if, while and try must be one single statement. Your "int i;" is not a statement but a declaration. By putting it in braces {}, those braces become a compound statement - the entire block is one statement within which you can basically start over. Both declarations and statements can go inside the block.
    Ashish Paliwal
    Greenhorn

    Joined: Oct 07, 2009
    Posts: 9
    So, if I write int i = 10;
    then this is a definition not a declaration. Should this work without braces of while loop ?
    Rahul P Kumar
    Ranch Hand

    Joined: Sep 26, 2009
    Posts: 188
    Ashish Paliwal wrote:So, if I write int i = 10;
    then this is a definition not a declaration. Should this work without braces of while loop ?


    good question.
    I think the problem is of scope of the variable 'i'. Local variable finds its scope within curly braces. When you declare or define 'i' in one line 'while' statement, 'while' has no problem, but JVM has problem fixing scope of 'i', while it can not attach scope of 'i' to curly braces of main(), it is unable to find braces for 'while' statement as well. This is why as soon as you are putting curly braces, everything works.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19720
        
      20

    No, because it's still not a statement. It is a declaration (int i) with immediate definition (= 10).
    Rahul P Kumar
    Ranch Hand

    Joined: Sep 26, 2009
    Posts: 188
    Rob Prime wrote:No, because it's still not a statement. It is a declaration (int i) with immediate definition (= 10).


    Can you explain what consists statement? I really need clarification about this term.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39409
        
      28
    Use the same Java Language Specification section I quoted earlier. Scroll up and read it. It is very accurate. It is very easy to read. The last sentence was a lie.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Local variable