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

Source: Internet
Author: User
Tags modifier modifiers volatile

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

Modifiers is keywords that is added to the change meaning of a definition. In Java, Modfiers is cateogrized into and types:

1. Access Control modifier

2. Non Access modifier

1) Access control modifier

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

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

2) non-access Modifier

Non-access modifiers don't change the accessibility of variables and methods, but they do provide them special properties . Non-access modifiers is of 5 types,

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

Final

Final field:its content cannot be changed, and it must was initialized when it was 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 and you cannot override it.

Static

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

Static variable:static variable is class member that can be accessed without instance of the class. Static Varibale have only one

Single storage. Static variable is initialized only once. Static variable represent common proiperty of a class.

Class Employee{int e_id; String Name;static string company_name = "Studytonight";}

The static Method:static method does not need instance of the class to access. Main () method is the most common example. Main () method

is declared as static because it's 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[] A RG)  {     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 is referenced from a static context?

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

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

Transient modifier

When a instance variable is declared as transient and then it value does not persist when a object is serialized

Synchronized modifier

When a method was synchronized it can be accessed by only one thread at a time.

Volatile modifier

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

Volatile variables is used in case of the multithreading program.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.