Access level of the class

Source: Internet
Author: User

Access level of the class:
Class access level is like a tree, want to reach the top of the tree, to climb slowly from under the tree. The branches under the tree can see the branches next to them. In Java, the methods of a class also have a level relationship.
Public access levels:
A class with the "public" modifier is declared as the access level of the State, that is, preceded by the name of the class with the publicly modifier. The class is decorated with the public modifier, which means that any class in any package can access the class. But pay attention to the problem of different packages. Although it can be accessed by any class in the package, reference statements are also required under different packages. The following code shows how to use the public modifier to decorate a class.
Create a Package A
Package A;

Import b.*;

The test class describes a modifier that modifies a class
Pubblic class Test
{
Main entry method of Java program
public static void Main (Strin[]args)
{
Creating an object instance of Test1
test1 T1 =new test1

Assigns the value of the EMP to the string type S
String s =t1.emp;

Print and display the results
System.out.println (s);

}
}
Create a Package
Package B;

public class Test1
{
String emp = "Member variable in different packages"
}

The default access level:

Classes with a default access level are declared without any modifiers, and the default access level can be treated as package-level access and can only be used by classes under the same package. The default access level is similar to the exposed access level, where the default access level does not make it possible to access classes under different packages, only those under the same package.

The test class describes the default level of class

Class Test

{

...//Method body

}

Create a Package A

Package A;

Import b.*;

public class Test

{

public static void Main (string[] args)

{

Test1=new test1 ();

String s=t1.emp;

System.out.println (s);

}

}

Create a Package

Package B;

Class Test1

{

Striing emp= "variables in different members"

}

Access level of the class

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.