Results 1 to 15 of 15
  1. #1
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Reg. Instance variables

    From the tutorial:
    Instance variables can be declared in class level before or after use.

    But the code below does not compile. Why?

    Java Code:
    public class MainApp {
    	i=5;
    	public static void main(String[] args) {
    		System.out.println("Hello World");
    	}
    	int i;
    }

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Reg. Instance variables

    Hi,

    Inside a class you can define fields and methods. You cannot put such statements inside.

    My understanding is, that you can do this:
    Java Code:
    class test {
       void test() {
         i = 5;
       }
    
       int i;
    }
    But NOT this:
    Java Code:
    public class Test {
      int i = a;
      int a = 5;
    }
    But you should show the tutorial to us so that we can get the full context.

    Konrad

  3. #3
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Reg. Instance variables

    Inside a class you can define fields and methods. You cannot put such statements inside.
    Sorry sir, You are wrong this time. Code 1 compiles fine

    Code 1:
    Java Code:
    public class MainApp {
     int a = 5;
    }
    But you should show the tutorial to us so that we can get the full context.

    Java - Variable Types

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,090
    Rep Power
    4

    Default Re: Reg. Instance variables

    Quote Originally Posted by jmohandos304 View Post
    Inside a class you can define fields and methods. You cannot put such statements inside.
    Sorry sir, You are wrong this time. Code 1 compiles fine

    Code 1:
    Java Code:
    public class MainApp {
     int a = 5;
    }
    Unfortunately for you, Konrad is right. The compiler complains about the line i=5. In your case you added "int" in front of "a", so the field is declared now, which makes Konrad's statement true: Inside a class you can define fields[...].
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Reg. Instance variables

    Oh, I misunderstood the context in what Konrad was saying. Now I understood.

  6. #6
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Reg. Instance variables

    Ok. Now the question arises. Why can instance variables be declared after use whereas static and local variable cannot be declared after use?

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,090
    Rep Power
    4

    Default Re: Reg. Instance variables

    What do you mean with "after use"? Java source code is not executed line by line from line 1 to line X. The following example which was given previously demonstrates this:

    Java Code:
    class Test {
       void myTestMethod() {
         i = 5;
       }
     
       int i;
    }
    The fact that "i=5" is on line 3, does not mean it's executed before the declaration at line 6. The compiler looks at the scope of all variables. Since the declaration of i is at a bigger scope than the usage, it's allowed.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  8. #8
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Reg. Instance variables

    How about static variables?

  9. #9
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Reg. Instance variables

    The core point with all this stuff is, that the compiler is not just going through the files from top to bottom (including other files if requested). That is a typical X / C++ compiler. So you cannot use a method when it is not declared so far. So you ended up with 2 different designs:
    - In C you often had a pattern where you declared all methods first. That way you could choose to implement these in any order
    - In C++ you split up declaration from implementation. So you have header files and source files with the code.

    In Java all this is not required. The Compiler will take care of that. I try to give you a quickl glance without going deep into how compiler could be build and how the java compiler is build - but please be aware that this is just a really rough thing that is not exact at all!

    As we learned: Compiler likes to have headers first. So a java compiler could go through the code and only get the declarations. So it is not interested in any code except the core declarations.
    - So the compiler would learn what classes (including inner classes) exist
    - The compiler would learn all elements (e.g. variables, methods, ....)
    And with that knowledge, the compiler can now do the core compilation. So if it sees an assignment, it will know if the variable is there or not. It does not matter if the declaration was written above or below the assignment in the source file.

    Local variables are completely different. Just look at the definition. A variable on class (static) or instance (non static) level is valid on that level. A local variable is valid from the point of the declaration up to the end of the block.
    That is the definition. And the compiler has to implement the definition. If that wouldn't be the case, the compiler would simply be buggy.

    Konrad

  10. #10
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Reg. Instance variables

    I read the whole of what you said.

    A local variable is valid from the point of the declaration up to the end of the block.

    A local variable is having method level scope. So what? Why cannot local variables appear in the code before its declaration?

  11. #11
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Reg. Instance variables

    Where do you get your stuff? Is that still any tutorial you found on the web?

    Let us be exact: When we want to see the exact definitions, then we simply have to refer to the Java Language Specification. All that is written in there should be true. If your compiler behaves different, then the compiler is buggy / not implementing the Java Language specification.

    JLS can be found at: The Java® Language Specification

    Local variables are defined in 14.4: Chapter*14.*Blocks and Statements
    Ok, regarding scopes we are just redirected to 6.3 and 6.4: Chapter*6.*Names

    So what do we find:

    The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement.

    You can look up everything in more detail if you like. I switch back to my own words right now:
    A block is normally { ...}. So you have
    Java Code:
    // Whatever comes before ...
    {
      // Here the local variables i and j are not declared ... 
      int i=5, j=i; // Here i is declared - starting with the initializer. So starting with i=5 the variable is defined. So we could use it on the same line again when declaring j.
      // Here we have local variables i, j. 
    } // End of block so now the variables are no longer valid.
    I hope this helped.

    Konrad

  12. #12
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Reg. Instance variables

    Where do you get your stuff? Is that still any tutorial you found on the web?

    Quoting the SCJP(OCPJP) book:

    https://java.net/downloads/jfjug/SCJ...0071591060.pdf

    Local variables are variables declared within a method. That means the variable is
    not just initialized within the method, but also declared within the method. Just
    as the local variable starts its life inside the method, it's also destroyed when the
    method has completed.


    So pls try to understand my question. Inside a method why a local variable cannot appear before its declaration if I declare the local variable inside the same method?

  13. #13
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Reg. Instance variables

    Because the java language specification defined it that way. That is the reason why I pointed you to it and even copied the important sentence.

    Why is it not allowed in a soccer game that one player hits the other and soon all player just hit each other? => because that are the rules.

    A java compiler follows the rules. So the JLS is the rulebook.

    Konrad

  14. #14
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Reg. Instance variables

    So the JLS is the rulebook.

    Ok. Thanks.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,029
    Rep Power
    6

    Default Re: Reg. Instance variables

    Something which was alluded to but not specifically stated is that compilers generally make more than one pass over the source code. The purpose is to take care of exactly what you are talking about. This allows them to resolve what might appear as potential conflicts or incorrect code to us "sequential beings" but in actuality is just a reordering of what is already allowed per the language specification.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Help with instance variables in a method
    By mstratmann in forum New To Java
    Replies: 3
    Last Post: 05-07-2013, 12:13 PM
  2. Instance variables
    By jwl in forum New To Java
    Replies: 2
    Last Post: 10-25-2012, 12:29 AM
  3. Instance Variables
    By TriSBR in forum New To Java
    Replies: 2
    Last Post: 11-16-2011, 05:16 PM
  4. Instance variables
    By lala in forum New To Java
    Replies: 1
    Last Post: 01-26-2011, 05:38 PM
  5. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 06:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •