Home>

java access modifier (access control)java uses modifiers to control access and other features of classes, properties, and methods,Usually placed at the very front of the statement.E.g:

public class classname {
  //body of class
}
private boolean myflag;
static final double weeks=9.5;
protected static final int boxwidth=42;
public static void main (string [] arguments) {
  //body of method
}

Java has many modifiers,Divided into access modifiers and non-access modifiers.This section only introduces access modifiers,Non-access modifiers are described later.

Access modifiers are also called access control characters.A keyword that controls the use rights of a class, member variable, or method.

In object-oriented programming,Access control is an important concept.You can use it to protect access to classes, variables, methods, and constructors.

Java supports four different access rights:

public:public

Classes, methods, constructors, and interfaces declared as public can be accessed by any other class.

If several public classes that access each other are distributed in unused packages,You need to import the package where the corresponding public class is located.Because of class inheritance,All public methods and variables of a class can be inherited by its subclasses.

The following method uses public access control:

public static void main (string [] arguments) {
  //body of method
}

The main () method of the java program must be set to public,Otherwise, the java interpreter will not be able to run the class.

protected:protected

Variables, methods, and constructors declared as protected can be accessed by any other class in the same package,It can also be accessed by subclasses in different packages.

protected access modifiers cannot modify classes and interfaces,Methods and member variables can be declared protected, but member variables and member methods of an interface cannot be declared protected.

Subclasses have access to the methods and variables declared by the protected modifier,This protects unrelated classes from using these methods and variables.

The following parent class uses the protected access modifier,The child class overrides the bark () method of the parent class.

public class dog {
  protected void bark () {
    system.out.println ("Wang Wang, don't come over");
  }
}
class teddy extends dog {//teddy
  void bark () {
    system.out.println ("Wang Wang, I'm so scared, don't follow me");
  }
}

If the bark () method is declared private, classes other than dog will not be able to access the method.If you declare bark () as public, all classes can access the method.If we only want the method to be visible to its subclasses,The method is declared as protected.

private:private

The private access modifier is the strictest access level,So the methods, variables and constructors declared as private can only be accessed by the class to which they belong.And classes and interfaces cannot be declared private.

Variables declared as private access types can only be accessed by external classes through public getter/setter methods in the class.

The use of private access modifiers is mainly used to hide class implementation details and protect class data.

The following classes use private access modifiers:

public class dog {
  private string name;
  private int age;
  public string getname () {
    return name;
  }
  public void setname (string name) {
    this.name=name;
  }
  public int getage () {
    return age;
  }
  public void setage (int age) {
    this.age=age;
  }
}

In the example, the name and age variables in the dog class are private variables.So other classes cannot directly get and set the value of this variable.To enable other classes to manipulate the variable,Two public methods are defined, getname ()/setname () and getage ()/setage (), which are used to get and set the value of a private variable.

this is a keyword in java,This chapter will talk about,You can click java this keyword for detailed preview.

Define methods in your class to access private variables,It is customary to name it like this:prefix the variable name with "get" or "set" and capitalize the first letter of the variable.For example, the method to get the private variable name is getname () and the method to set name is setname (). These methods are often used,Also has a specific title,Called getter and setter methods.

Default:Do not use any keywords

Properties and methods declared without any modifiers,Visible to classes in the same package.The variables in the interface are implicitly declared as public static final, and the methods in the interface have public access by default.

As shown in the example below,Class, variable, and method definitions use no modifiers:

class dog {
  string name;
  int age;
  void bark () {//barking
    system.out.println ("Wang Wang, don't come over");
  }
  void hungry () {//hungry
    system.out.println ("Master, I'm hungry");
  }
}

Access control and inheritance

Please note the following method inheritance (readers who do not understand the concept of inheritance can skip here,Or click on java inheritance and polymorphic preview) rules:

Methods declared as public in the parent class must also be public in the child class.

Methods declared as protected in the parent class are either declared as protected in the subclass or public. Cannot be declared as private.

Methods declared by the default modifier in the parent class,Can be declared private in a subclass.

Methods declared as private in the parent class cannot be inherited.

How to use access control characters

The access control allows us to easily control the permissions of the code:

When you need to make your own class accessible to all other classes,You can declare the access control of the class as public.

When you need to make your class accessible only by classes in your own package,You can omit the access control character.

When you need to control member data in a class,The member data access control in this class can be set to public, protected, or omitted.

Scope of java variables

In Java, the scope of variables is divided into four levels:class level, object instance level, method level, and block level.

Class-level variables are also called global-level variables or static variables.Need to use the static keyword decoration,You can compare this with static variables in c/c++.Class-level variables already exist after the class is defined,Take up memory space,Can be accessed by class name,No instantiation is required.

Object instance-level variables are member variables,Memory space is allocated only after instantiation.To access.

Method-level variables are variables that are defined inside a method.Are local variables.

Block-level variables are variables defined inside a block.The lifetime of the variable is this block,After this block disappears,For example, if, for blocks.A block is code surrounded by braces,E.g:

{
  int age=3;
  string name="www.weixueyuan.net";
  //Correct, age and name variables are accessible inside the block
  system.out.println (name + "already" + age + "aged");
}
//Error, age and name variables cannot be accessed outside the block
system.out.println (name + "already" + age + "aged");

Explanation:

In addition to accessing method-level variables within a method,You can also access class-level and instance-level variables.

Block-level access to class-level and instance-level variables,If the block is contained inside a method,It also has access to method-level variables.

Method-level and block-level variables must be explicitly initialized,Otherwise it cannot be accessed.

Demo code:

public class demo {
public static string name="Wei Xue Yuan";//class-level variable
public int i;//object instance-level variable
//Property block, run when the class initializes properties
{
int j=2;//block-level variables
}
public void test1 () {
int j=3;//method-level variables
if (j == 3) {
int k=5;//block-level variables
}
//Block-level variables cannot be accessed here,Block-level variables can only be accessed inside the block
system.out.println ("name =" + name + ", i =" + i + ", j =" + j);
}
public static void main (string [] args) {
//do not create objects,Access class-level variables directly by class name
system.out.println (demo.name);
//method of creating object and accessing it
demo t=new demo ();
t.test1 ();
}
}

operation result:

Wei Xue Yuan
name=Wei Xue Yuan, i=0, j=3
  • Previous In-depth analysis of jsonp protocol principle
  • Next In-depth analysis of Java packages