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.

时间: 2024-12-27 19:07:13

Java Modifiers, default/public/protected/private/ final/static/transient/synchronized/volatile的相关文章

java中的public,protected,private权限修饰

在java程序前如果不加任何一个权限修饰符,默认是只允许包内访问,只有在同一个包内的类之间才能访问. public: 对每个人都是可用的,在任何地方的任何包内都可以访问. protected: protected处理的是继承的问题,只有子类和自己可以访问,不局限于本包. private: 只有自己可以访问.

JAVA修饰符类型(public,protected,private,friendly)

JAVA修饰符类型(public,protected,private,friendly) public的类.类属变量及方法,包内及包外的不论什么类均能够訪问:protected的类.类属变量及方法,包内的不论什么类,及包外的那些继承了此类的子类才干訪问:private的类.类属变量及方法,包内包外的不论什么类均不能訪问:假设一个类.类属变量及方法不以这三种修饰符来修饰,它就是friendly类型的,那么包内的不论什么类都能够訪问它,而包外的不论什么类都不能訪问它(包含包外继承了此类的子类),因此

[学习笔记]Java的访问指示符public,protected,private,缺省的作用域

0.引言 Java的访问指示符public,protected,private,缺省可以用来修饰类和方法. 1.作用域如下 public: 其它类都能访问此类或方法 protected: 为继承而生的,子类可以访问父类该修饰的方法 private: 为类自己所用,一般修饰成员变量 缺省: 同一个包内可用("友好"类) 2.同一个java文件里面若有多个类 (1) 每个编译单元(文件)都只能有一个public 类.每个编译单元有一个公共接口的概念是由那个公共类表达出来的.根据自己的需要,

14 PHP 类的继承 [public protected private] parent 构造方法 析构方法 重写 最终类和方法 设计模式

类的继承 简单理解: 某个类A具有某些特征,另一个类B,也具有A类的所有特征,并且还可能具有自己的更多的一些特征,此时,我们就可以实现:B类使用A的特征信息并继续添加自己的一些特有特征信息. 基本概念 继承:一个类从另一个已有的类获得其特性,称为继承. 派生:从一个已有的类产生一个新的类,称为派生. 继承和派生,其实只是从不同的方向(角度)来表述,本质上就是一个事情. 父类/子类:已有类为父类,新建类为子类.父类也叫"基类",子类也叫"派生类" 单继承:一个类只能从

C++:继承访问属性(public/protected/private)

? 公有继承(public) 公有继承在C++中是最常用的一种继承方式,我们先来看一个示例: 1 #include<iostream> 2 using namespace std; 3 class Father{ 4 public: 5 Father()=default; 6 void Father_show1(){ 7 cout<<"调用Father类的public方法:Father_show1"<<endl; 8 } 9 protected: 1

c++ public protected private 继承

1:访问控制 1.1 public 这类型成员可以被类本身函数访问,也可以被外部创建的类对象调用.子类对象与子类内部可以访问 1.2 protected类型成员,只能被类本身函数访问.外部创建的类对象没有访问权限.子类对象没有访问权限,子类内部可以访问 1.3 private类型成员,只能被类本身函数访问,外部创建的类对象没有访问权限.子类对象和子类内部都没有访问权限 class TestObject{ public: void Function(); void TestPublic(); pr

C++中public,protected,private访问

总结:1. protected和private的区别在于 ,protected的访问标号限制,能够被派生类的成员函数访问,而private不能2. protected和publice的区别在于,protected的访问限制,使其不能被基类的对象访问. -----------------------------------------------------第一:private, public, protected 访问标号的访问范围.private:只能由1.该类中的函数.2.其友元函数访问.不

public, protected, private,internal,protected internal的区别

虽然这个知识比较简单, 但是老是会忘, 写上来, 增强记忆. 在C#语言中,共有五种访问修饰符:public.private.protected.internal.protected internal.作用范围如下表: 访问修饰符 说明public 公有访问.不受任何限制.private 私有访问.只限于本类成员访问,子类,实例都不能访问.protected 保护访问.只限于本类和子类访问,实例不能访问.internal 内部访问.只限于本项目内访问,其他不能访问.protected inter

php public protected private属性实例详解

php 类中函数和类变量都有三个属性:public protected private,具体什么时候使用什么属性好纠结,特意找了个实例,这样看起来更清晰. public 表示全局,类内部外部子类都可以访问:private 表示私有的,只有本类内部可以使用:protected 表示受保护的,只有本类或子类或父类中可以访问: <?php//父类class father{ public function a(){ echo "function a"; } private functio