The Artima Developer Community
Sponsored Link

Java Buzz Forum
Static Members in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Static Members in java Posted: Apr 19, 2015 1:40 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Static Members in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • The class level members which have static keyword in their definition are called static members.

Types of Static Members:

  •  Java supports four types of static members
  1. Static Variables
  2. Static Blocks
  3. Static Methods
  4. Main Method (static method)

  • All static members are identified and get memory location at the time of class loading by default by JVM in Method area.
  • Only static variables get memory location, methods will not have separate memory location like variables.
  • Static Methods are just identified and can be accessed directly without object creation.

1.Static Variable:

  • A class level variable which has static keyword in its creation statement is called static variable.

  1. package com.instanceofjava;
  2. class Demo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  
  7. }

  • We can not declare local variables as static it leads to compile time error "illegal start of expression".
  • Because being static variable it must get memory at the time of class loading, which is not possible to provide memory to local variable at the time of class loading.

  1. package com.instanceofjava;
  2. class Demo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  public static void main(String [] args){
  7.  
  8.    //local variables should not be static
  9.  static int a=10;// compile time error: illegal start of expression

  10. }


  • All static variables are executed by JVM in the order of they defined from top to bottom.
  • JVM provides individual memory location to each static variable in method area only once in a class life time.

Life time and scope:

  •  Static variable get life as soon as class is loaded into JVM and is available till class is removed from JVM or JVM is shutdown.
  • And its scope is class scope means it is accessible throughout the class.

  1. package com.instanceofjava;
  2. class StaticDemo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  
  7.  public static void main(String [] args){
  8.  
  9.    System.out.println("a="+a);
  10.    System.out.println("a="+b);
  11.    show();

  12.  }
  13.  
  14. public static void show(){
  15.  
  16.    System.out.println("a="+a);
  17.    System.out.println("a="+b);
  18. }

  19. }

static methods JVM architecture

Duplicate Variables:

  • If multiple variables are created with same name are considered as duplicate variables.
  • In the same scope we can not create multiple variables with same name.
  • If we create it leads to compile time error "variable is already defined".
  • Even if we change data type or modifier or its assigned value we can not create another variable with same name.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  
  5. static int a=10;
  6. int a=30;// Compile time error 
  7.  
  8. }

  • But it is possible to create multiple variables with same name in different scopes.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;
  5. int a=30;// Compile time error
  6.  
  7. public static void main(String [] args){
  8.   
  9. // it is allowed to define "a" in this method.
  10. int a=20;
  11.  
  12. }
  13. }

Shadowing:

  • It is possible to create local variables or parameters with same variable name.
  • The concept is called shadowing . It means local variable is a shadow of class level variable.
  • It means when you access it in side method , you will get local variables value but not  from class level.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.   

  8.  int a=20;
  9.  System.out.println("a="+a): // prints 20

  10. }
  11. }

 Local preference:

  • When we call a variable compiler and JVM will search for its definition in that method first, if its definition is not found in that method then will search for its definition at class level.
  • If its definition not found at class level also then compiler throws error: can not find symbol.
  • This phenomenon is called local preference.


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.  
  8.  System.out.println("a="+a): // prints 10 : method level no definition so access class level

  9.  int a=20;
  10.  System.out.println("a="+a): // prints 20 :method level variable found

  11. }
  12. }


  • By using class name we can get class level variables value.


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.  
  8.  System.out.println("a="+a): // prints 10 : method level no definition so access class level

  9.  int a=20;
  10.  System.out.println("a="+a): // prints 20 :method level variable found

  11.  System.out.println("a="+StaticDemo.a): // prints 10 : class level variable
  12.  
  13. }
  14. }




Read: Static Members in java

Topic: Interface Evolution With Default Methods – Part II: Interfaces Previous Topic   Next Topic Topic: XPath ancestor example

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use