Java Modifiers, default/public/protected/private/ final/static/transient/synchronized/volatile

reference: http://www.studytonight.com/java/modifier-in-java.php

 

Modifiers are keywords that are added to change meaning of a definition. In java, modfiers are cateogrized into two types:

1. Access control modifier

2. Non Access modifier

 

1) Access control modifier

Java language has four access modifier to control access levels for classes, variable methods and constructor.

  • Default : Default has scope only inside the same package
  • Public : Public scope is visible everywhere
  • Protected : Protected has scope within the package and all sub classes
  • Private : Private has scope only within the classes

 

 

2) Non-access Modifier

Non-access modifiers do not change the accessibility of variables and methods, but they do provide them special properties. Non-access modifiers are of 5 types,

  1. Final
  2. Static
  3. Transient
  4. Synchronized
  5. Volatile

 

final: 

final field: its content cannot be changed, and it must be initialized when it is declared.

final class: cannot be inherited. String class in java.lang packages is a example of final class

final method: method declared as final can be inherited but you cannot override it.

 

static:

static modifiers are used to create class variable and class methods which can be accessed without instance of a class

static variable: static variable are class member that can be accessed without instance of the class. Static varibale has only one 

        single storage. static variable are initialized only once. static variable represent common proiperty of a class. 

 

class Employee
{
int e_id;
String name;
static String company_name = "StudyTonight";
}

static method: static method do not need instance of the class to access. main() method is the most common example. main() method

is declared as static because it is called before any object of the class is created.

class Test 
{
 
 public static void square(int x) 
 {
  System.out.println(x*x);
 }

 public static void main (String[] arg) 
 {
   
  square(8)   //static method square () is called without any instance of class.
 }
}

 

 

static block: static block is used to initialize static data member, static block executes before main() method

class ST_Employee
{
   int eid;
   String name;
   static String company_name;
    
   static {
    company_name ="StudyTonight";    //static block invoked before main() method	 
    }

    public void show()
    {
        System.out.println(eid+" "+name+" "+company_name);
    }
    public static void main( String[] args )
    {
     ST_Employee se1 = new ST_Employee();
     se1.eid = 104;
     se1.name = "Abhijit";
     se1.show();
     
    }
}

 

Q. Why a non-static variable cannot be referenced from a static context ?

This is because non-static variables are related with instance of class and they get created when instance of a class is created by using new 

operator. So if you try to access a non-static variable without any instance compiler will complain because those variables are not yet created and they dont have any existence until an insatnce is created and associated with it

 

Transient modifier

when an instance variable is declared as transient, then its value does not persist when an object is serialized

 

Synchronized modifier

when a method is synchronized it can be accessed by only one thread at a time.

 

Volatile modifier

volatile modifier tells the compiler that the volatile variable can be changed unexpectedly by other parts of your program. 

Volatile variables are used in case of multithreading program.

 

转载于:https://www.cnblogs.com/morningdew/p/5618844.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值