Using a Instance/Static/Local variable before declaring it.
Summary
- Scope of Local variables starts from its declaration statement and it goes upto end of the Block where it is declared
- Local variables must be accessed from or after declaration statement.
- Class Level variables can be declared anywhere in the class. It can be accessed from other members.
- You can access class level variable from method, constructors directly.
- When static variables declared after static initialization block then remember following:
- You can initialize the variable directly.
- You cannot access the value of the variable directly.
- When you access value of variable directly, compiler will generate error called : "Illegal forward reference error"
- You can access the value of the variable with class name.
- When instance variables declared after instance initialization block then remember following points:
- You can initialize the variable directly.
- You cannot access the value of the variable directly.
- When you access value of the variable directly, compiler will generate error called "Illegal forward reference"
- You can access the value of the variable with this keyword.
- When you declare instance variables and initialization blocks then initialization of the variables and execution of the initialization blocks will be in the same order as defined in the class.
- final instance variable can be initialized from the constructor also but it must be initialized from all the constructors if available.
- When final instance variable initialized from the IIB then that variable cannot be initialized from constructor.
- final static variable must be initialized from SIB only; it can't be initialized from the constructors or IIB.
- Class loading is the process of reading the required class file and storing that information in the memory.
- Class will be loaded only once in the memory by the JVM. After loading the class if you delete the class file then also the application will be executed.
- Class will be loaded in the memory by the JVM when first time the member of the class will be used by JVM:
- When you are executing the class with java.exe then class will be loaded and static block will be executed. From Java 7, if you don't have main method, then class won't be loaded and static block won't be executed.
- When you create the object of the class and before this statement no other member of the class is used, then class will be loaded.
- When you access static members of the class and before this statement no other member of the class is used then class will be loaded.
- When you access the final static variable that is initialized in the same statement then JVM wont execute the static block.
class Lab{
public static void main(String args[]){
System.out.println(Hello.a);
}
}
class Hello{
final static int a = 10;
static{
System.out.println("ST Block");
}
}
- When you define the reference variable but not using any members of the class then class will not be loaded.
class Lab{
public static void main(String args[]){
Hello h = null;
}
}
Example 1:
Accessing a variable before declaration:
class Lab25{
public static void main(String args[]){
System.out.println(a);
int a = 10;
}
static{
System.out.println(b);
int b = 10;
}
}
/*
Lab25.java:3: error: cannot find symbol
System.out.println(a);
^
symbol: variable a
location: class Lab25
Lab25.java:8: error: cannot find symbol
System.out.println(b);
^
symbol: variable b
location: class Lab25
2 errors
*/
Example 2:
Value can be assigned to the variable before declaring, but cannot be accessed (using print statements)
class Lab27{
public static void main(String args[]){
System.out.println(Hello.b);
}
}
class Hello{
static{
b = 10;
Hello.b=10; //either of the two statements are valid
}
static int b = 90;
}
/*
output
90
(Since the static blocks/variables are executed in the order as they are written.)
No C.T.E in line - b=10 since here value is assigned to the variable.
*/
Example 3
Illegal forward reference error when variable is used directly.
class Lab29{
public static void main(String args[]){
System.out.println(Hello.b);
}
}
class Hello{
static{
System.out.println(b);
}
static int b = 90;
}
/*
Lab29.java:11: error: illegal forward reference
System.out.println(b);
^
1 error
*/
Example 4
When a static variable is used using class reference, Illegal forward reference error is not thrown.
class Lab30{
public static void main(String args[]){
System.out.println("Main :"+Hello.b);
}
}
class Hello{
static{
System.out.println("SB:"+Hello.b);
}
static int b = 20;
}
/*
SB:0
Main:20
*/
Example 5:
Illegal forward reference error. Cannot reference a field before its defined.
class Lab33{
public static void main(String args[]){
System.out.println("Main :"+Hello.b);
}
}
class Hello{
static int a=10;
static{
System.out.println("SB1:"+a);
System.out.println("SB1:"+b); //cannot access a field before it's declared.
}
static int b = 20;
static{
System.out.println("SB2:"+a);
System.out.println("SB2:"+b);
}
}
/*C.T.E
Lab33.java:13: error: illegal forward reference
System.out.println("SB1:"+b); //cannot access a field before it's declared.
^
1 error
*/
Example 5:
Static variable can be accessed using Class name before declaration.
class Lab34{
public static void main(String args[]){
System.out.println("Main :"+Hello.b);
}
}
class Hello{
static int a=10;
static{
System.out.println("SB1:"+a);
System.out.println("SB1:"+Hello.b);
}
static int b = 20;
static{
System.out.println("SB2:"+a);
System.out.println("SB2:"+b);
}
}
/*output:
SB1:10
SB1:0
SB2:10
SB2:20
Main:20
*/
Example 6 :
Value can be assigned to an instance variable before declaration directly or using "this" keyword.
class Lab35{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
{
a=90;
this.a=90;//can be used without any error.
}
int a = 20;
}
/*
output:
20
*/
Example 7:
Cannot access an instance variable directly before declaring it.
class Lab37{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
{
System.out.println(a); //cannot access variable before it is declared.
}
int a = 20;
}
/*
Lab37.java:12: error: illegal forward reference
System.out.println(a); //cannot access variable before it is declared.
^
1 error
*/
Example 8:
An instance variable can be used (print statement) using the "this" keyword before declaring it.
class Lab38{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
{
System.out.println(this.a);
}
int a = 20;
}
/*
output:
0
20
*/
Example 9:
Accessing an instance variable inside an Instance block before declaring it.
class Lab41{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
int a = 10;
{
System.out.println("IB1:"+a);
System.out.println("IB1:"+b); //cannot access variable before its declared.
}
int b = 20;
{
System.out.println("IB2:"+a);
System.out.println("IB2:"+b);
}
}
/*
C.T.E
Lab41.java:14: error: illegal forward reference
System.out.println("IB1:"+b); //cannot access variable before its declared.
^
1 error
*/
Example 10:
Accessing an instance variable using "this" keyword inside an Instance block before declaring it.
class Lab42{
public static void main(String args[]){
Hello h = new Hello();
System.out.println("Main :"+h.a);
}
}
class Hello{
int a = 10;
{
System.out.println("IB1:"+a);
System.out.println("IB1:"+this.b);
}
int b = 20;
{
System.out.println("IB2:"+a);
System.out.println("IB2:"+b);
}
}
/*
IB1:10
IB1:0
IB2:10
IB2:20
Main:10
*/
Example 11:
Instance Initializer blocks/statements(declarations) are always called before the Constructor is called.
class Lab44{
public static void main(String args[]){
Hello h = new Hello();
System.out.println("Main :"+h.a);
}
}
class Hello{
{
System.out.println("IIB: "+this.a);
}
Hello(){
System.out.println("Con: "+a);
}
int a = 10;
}
/*
output :
IIB:0
Con:10
Main:10
*/
Example 12:
Order of execution of Static blocks, Instance blocks and Static initializers
class Lab51{
public static void main(String args[]){
System.out.println("Main :"+Hello.a);
}
}
class Hello{
static int a = 10;
static Hello h1 = new Hello();
{
System.out.println("IB:");
}
static{
System.out.println("SB:");
}
static Hello h2 = new Hello();
}
/*
IB:
SB:
IB:
Main:10
*/
Example 13:
Blank final variable
class Lab58{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
final int a;
}
/*
C.T.E
Blank final variable is not allowed.
Lab58.java:10: error: variable a not initialized in the default constructor
final int a;
^
1 error
*/
Example 14:
Blank instance final variable can be initialized inside a constructor, but if there are more than 1 constructors, then it has to be initialized in all of them.
class Lab60{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
final int a;
Hello(){
a=10;
}
Hello(int a){}
}
/*
C.T.E
final variable is initialized only in 1 constructor and not in the other constructor.
*/
Example 15:
Blank final variable can be initialized using the this keyword also inside the constructors.
class Lab61{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
final int a;
Hello(){
a=10;
}
Hello(int a){
this.a=a;
}
}
/*
10
*/
Example 16:
Blank final variable cannot be initialized in both the constructors and IIB's. It should be initialized either in all constructors or in IIB.
class Lab62{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
final int a;
Hello(){
a=10;
}
{
a=90;
}
}
/*
C.T.Exception
Lab62.java:12: error: variable a might already have been assigned
a=10;
^
1 error
Since IIB is executed before a constructor is executed.
*/
Example 17:
Blank static final variable cannot be initialized inside IIB's or Constructor.
class Lab63{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
static final int a;
{
a=90;
}
}
/*
C.T.E
Lab63.java:12: error: cannot assign a value to final variable a
a=90;
^
1 error
*/
Example 18:
class Lab64{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
static final int a;
Hello(){
a=90;
}
}
/*
Lab64.java:12: error: cannot assign a value to final variable a
a=90;
^
1 error
*/
Example 19:
Blank static final variable can be initialized inside Static block.
class Lab66{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
static final int a;
static {
System.out.println("St block: "+Hello.a);
a=10;
}
}
/*
St block: 0
10
*/
Example 20:
class Lab68{
public static void main(String args[]){
Hello h = new Hello();
System.out.println(h.a);
}
}
class Hello{
static final int a;
static {
a=10;
System.out.println("St block: ");
}
}
/*
St block
10
*/
No comments:
Post a Comment