[Learn JAVA from scratch | Article 16] Introduction to Miscellaneous Knowledge Points

Table of contents

Foreword:

Bag:

final:

Permission modifiers:

Summarize:


Foreword:

This article does not belong to the text sequence, but introduces some commonly used words in object-oriented, which is convenient for everyone to understand and remember. This article will introduce what package, final, and permission modifiers are one by one.

Bag:

In Java, a package can be understood as a way to organize code, which can organize related classes together for easy management and use.

A package contains multiple classes, interfaces and other packages, the syntax is:

package packagename;

Where packagename is the name of the package, usually in reverse order of the domain name.

For example, Java provides java.lang package, which contains the most basic types and objects in Java, such as String class, System class, Object class and so on.

In Java, if we want to use a class or interface in a package, we need to use the import statement to import it into our code, for example:

import java.util.ArrayList;
import java.util.List;

The above code imports the ArrayList and List classes in Java's util package into our code, and we can use them directly.

In Java, the role of a package is not only to organize code, but also to control access rights and resolve naming conflicts . It is a very important concept in Java programming.

1. Control access rights

In Java, packages can also be used to control access permissions. There are 4 kinds of access modifiers in Java, they are public, protected, default (also called package-private) and private, which represent different access scopes. Access to classes, methods, and variables can be controlled by using package names to group different classes.

For example, we have two classes A and B, class A is in package com.example and class B is in package com.example.other . If we want the method in class B to be accessed only within the com.example.other package, we should add a default access modifier to the method. Note that there are no public, protected or private modifiers here:

package com.example.other;

public class B {
    void doSomething() {
        // do something
    }
}

This way, classes in other packages cannot access the doSomething method in class B.

2. Resolving naming conflicts

In Java, there may be classes with the same name, in the same package or in different packages. In order to solve this problem, Java uses the method of package name plus class name to uniquely identify a class.

For example, suppose we have two custom classes both named Test, one in package com.example.test1 and the other in package com.example.test2. How do we access these two classes in code? Just use the full class name (package name plus class name):
 

com.example.test1.Test test1 = new com.example.test1.Test();
com.example.test2.Test test2 = new com.example.test2.Test();

In this way, the two classes with the same name can be clearly distinguished.

In short, a package is a very important concept in Java. It can be used to organize code, control access rights, and resolve naming conflicts. It is an indispensable part of Java programming.

final:

In Java, the final keyword can be used to modify variables, methods, and classes. Its main functions are as follows:

1. Modify variables:

  • When final modifies a variable of a basic type, it means that the value of the variable cannot be changed, that is, a constant;
  • When final modifies a variable of reference type, it means that the reference of the variable cannot be changed, but the content of the object it refers to can be changed.

For example:


final int a = 1; // 常量a
final String str = "hello"; // 常量str,不能再引用其他字符串了
final List<String> list = new ArrayList<>(); // 常量list,不能再引用其他List了
list.add("world"); // 可以改变list所引用的对象的内容

2. Modification method:

When modifying a method as final, it means that the method cannot be overridden by subclasses.

For example:

class A {
    final void print() {
        System.out.println("A");
    }
}
class B extends A {
    // 编译错误:'print()' cannot override 'print()' in 'A' 
    // overridden method is final
    void print() { 
        System.out.println("B");
    }
}

3. Decoration class:

When final modifies a class, it means that the class cannot be inherited.

For example:


final class A {
    // ...
}
class B extends A { // 编译错误:cannot inherit from final 'A'
    // ...
}

The final keyword is used frequently in Java and can be used to ensure that the value of a constant remains unchanged, and that methods and classes are not changed, thereby ensuring the security and stability of the code. Common application scenarios include:

  • Declare constants;
  • Methods to prevent being overridden;
  • Prevent classes from being inherited;
  • Provide initial values ​​for instance fields;
  • Make sure method parameters and return values ​​are immutable.

Permission modifiers:

In Java, access modifiers are used to control the visibility of classes, interfaces, methods, and attributes to other codes. There are four access modifiers in Java, which are public, protected, default (also known as package-private) and private. The four modifiers are described in detail below:

1. public

Public is the most extensive access modifier, which means that the modified class, method, and attribute can be accessed by all other classes of code, that is, open all permissions.

2. protected

protected means that the modified methods and properties can be accessed by the current class, classes in the same package, and subclasses that inherit the current class, that is, protected permissions.

3. default (also known as package-private)

default means that the modified class, method, and attribute can only be accessed by code in the current package, that is, the default permission in the same package.

4. private

private means that the modified method and attribute can only be accessed by other methods inside the current class, that is, the highest private permission.

Access modifiers can be used in the following places:

  • Classes: public and default
  • Member variables: public, protected, default, and private
  • Constructors: public, protected, and default
  • Member methods: public, protected, default, and private
  • Interface: public and default
  • Interface member variables: public, static, final
  • Interface member methods: public, default, static and abstract

Summarize:

This article introduces three common knowledge points in our object-oriented programming. I hope you can understand the definitions of these three well. Only in this way can you better master the JAVA language.

If my content is helpful to you, please like, comment and bookmark . Creation is not easy, everyone's support is my motivation to persevere!

Guess you like

Origin blog.csdn.net/fckbb/article/details/131354032