• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance Initializer

 
Muhammad Rehan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why this code compiles

and not this

I am unable to figure out if instance initializers requires variable to be declared before it can be used then why first part of my code compiles?

 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use instance variable on the left hand side of an assignment operator in an instance initializer though instance variable declarations appear textually after the use.

In your second example here s1 is used at left hand side of assignment so no problem It compiles but on the very next line this use is not allowed so gives compile time error.
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to CodeRanch!

As you are new here, I edited your post to put code tags around your code. Always UseCodeTags when posting code with proper indentations. See now It looks more readable
 
Muhammad Rehan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ganesh,

In case of assignment we can use instance variables but as a statement we cannot i guess.
Helped me alot;
cheers.
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I always thought that this rule was weird.

So, you are not allowed to access a variable before it is declared. I get that. However, you can assign the variable? Isn't assigning a variable accessing a variable? Heck, isn't it also changing the value of the variable too?

Henry
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad Rehan wrote: but as a statement we cannot i guess.
If you want to use that instance variable in print statement then need to use with this keyword like
which will print "someText" as value of String s1.

Henry Wong wrote:However, you can assign the variable? Isn't assigning a variable accessing a variable? Heck, isn't it also changing the value of the variable too? 
Yes.
Before declaration we can assign the instance variable using It''s name only i.e. left side of assignment operator but when we need to read It may be for some operations like printing Or arithmetic operation in that situation we have to use this keyword with instance variable. In case of static variable we have to use ClassName.staticVariable

  • There are 4 precise rules governing forward reference to instance variables i.e. forward references during field initialization, If these are met then forward usage of instance variables will give compile time error even though these instance variables are in scope.

  • 8.3.3. JLS 8 wrote:Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. Specifically, it is a compile-time error if all of the following are true:

    1. The declaration of an instance variable in a class or interface C appears textually after a use of the instance variable;

    2. The use is a simple name in either an instance variable initializer of C or an instance initializer of C;

    3. The use is not on the left hand side of an assignment;

    4. C is the innermost class or interface enclosing the use.
  • If we look at a line of code at 4 we are trying to assign value of no1 to no2 means we need to read value of no1 but we are using only name of instance variable which is not allowed according to 4 rules so gives compile time error saying "illegal forward reference"
  • Same with 6, 7 where trying to increment value of no1.
  • But at 1, 2 and 8 here we are also trying to read value of no1 for printing, incrementing and multiplication but here It's not just simple name of instance variable no1 but used with this keyword so no problem.
  • At 9 It occured in a different class so no problem.


  • If we remove/comment line of codes causing CE then run this code, we get following output:
    no1: 0
    no1: 1
    no1: 120
    no2: 200

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic