Recommended courses: Java tutorial
1. First, we need to figure out what a class is? What is an object?
Class - is abstract. It is an abstraction (or collection) of objects with common attributes and services, which includes two main parts: attributes and services;
Object - is concrete. It is an entity used to describe objective things in the system. It is a basic unit that constitutes the system.
The relationship between classes and objects is like the relationship between molds and castings. The instantiation result of a class is an object, and the abstraction of a type of object is a class.
2. Definition of class
The general format for defining a class in Java: modifier class class name {member}
Understanding: The modifier is Optional, with public or not;
class——keyword, must have;
class name—— The first letter is capitalized, and the name is based on the camel case naming rule. It must have;
members - it has member attributes and member methods, which will be explained in detail below.
3. Member (fields) definition
1. Member attribute definition
格式:[ public | private | protected ] [ static ] [ final ] 类型 成员名
Understanding: Type and member name are Required, others are optional;
public public members - can also be accessed by different packages;
private private members - only Can be accessed in this class;
protected protected members - accessible in the same package and subclasses;
Do not add the above three Default - accessible in the same package;
static
Static member - belongs to the class, it does not belong to a specific object, and all objects of the class share this member. All objects that are not static objects must be instantiated before they can be accessed.
Static members or methods are accessed directly through the class name (via: class name. reference);
final member - the final member, whose The value cannot be changed;
final class - the final class, cannot be inherited;
final method - the final method, this method cannot Rewritten;
Note: final and public can be loaded on the class, but static cannot.
2. Member method definition
格式:[public|private|protected] [static] [final] 类型 方法名(形参){ 方法体; return表达式; }
Understanding:
Type (first letter capitalized) (required): refers to the type of return value (either It is a basic type, or it can be an object type);
Method name (required): what to do;
Method body: defines the method The specific content usually plays two roles: one is to perform various operations around the attributes of the class, and the other is to perform data exchange and message passing operations with other classes and objects;
Formal parameters (optional): There are only two types: basic type and object type. Parameters in method calls are used to pass values and references. At the same time, methods can also be nested and called recursively;
return can not only return a value, but also end the method body. It is worth noting Yes: If a non-void return type is specified in the method body, the method must include a return statement to ensure that a value is returned under any circumstances. The return statement cannot be followed by any expression.
Note: Defining another method in a method will produce a syntax error (methods cannot be nested, classes can). It is best to avoid local variables "shielding" instance (member) variables, which are not defined in a class. This can be done by using an identifier with the same name.
If the member variable with the same name is blocked and you want to access the member variable again, you can use this.member variable
.
Understanding:
Local variables: variables declared or defined in a method. Variables are created when this method is called and when this method is executed. Variables are released;
Instance variables: variables declared or defined outside all methods but in the class body, the valid scope is the entire class;
-
Shielding is not overwriting
4.Now a class is created. Of course, you can define a class that only contains member variables or only methods. If If a class has only member variables, then this class cannot do anything. In order for the class to do things, methods must be defined for this class. Because class methods can be called by other classes through objects, they are also called interfaces of classes.
5. A brief example is as follows:
public class Student { //创建了一个名为Student的公共类 public String name;//定义name属性 在String前加上public,则其他包也能调用此属性,不加则无法调用 public void study(){ System.out.println("我是学生"+name+",正在学习"); } //写一个公共方法study() public static void hi(){ System.out.println("hi,everybody!"); } //写一个静态的公共方法hi() }
The above is the detailed content of How to create a java class. For more information, please follow other related articles on the PHP Chinese website!

JVMmanagesgarbagecollectionacrossplatformseffectivelybyusingagenerationalapproachandadaptingtoOSandhardwaredifferences.ItemploysvariouscollectorslikeSerial,Parallel,CMS,andG1,eachsuitedfordifferentscenarios.Performancecanbetunedwithflagslike-XX:NewRa

Java code can run on different operating systems without modification, because Java's "write once, run everywhere" philosophy is implemented by Java virtual machine (JVM). As the intermediary between the compiled Java bytecode and the operating system, the JVM translates the bytecode into specific machine instructions to ensure that the program can run independently on any platform with JVM installed.

The compilation and execution of Java programs achieve platform independence through bytecode and JVM. 1) Write Java source code and compile it into bytecode. 2) Use JVM to execute bytecode on any platform to ensure the code runs across platforms.

Java performance is closely related to hardware architecture, and understanding this relationship can significantly improve programming capabilities. 1) The JVM converts Java bytecode into machine instructions through JIT compilation, which is affected by the CPU architecture. 2) Memory management and garbage collection are affected by RAM and memory bus speed. 3) Cache and branch prediction optimize Java code execution. 4) Multi-threading and parallel processing improve performance on multi-core systems.

Using native libraries will destroy Java's platform independence, because these libraries need to be compiled separately for each operating system. 1) The native library interacts with Java through JNI, providing functions that cannot be directly implemented by Java. 2) Using native libraries increases project complexity and requires managing library files for different platforms. 3) Although native libraries can improve performance, they should be used with caution and conducted cross-platform testing.

JVM handles operating system API differences through JavaNativeInterface (JNI) and Java standard library: 1. JNI allows Java code to call local code and directly interact with the operating system API. 2. The Java standard library provides a unified API, which is internally mapped to different operating system APIs to ensure that the code runs across platforms.

modularitydoesnotdirectlyaffectJava'splatformindependence.Java'splatformindependenceismaintainedbytheJVM,butmodularityinfluencesapplicationstructureandmanagement,indirectlyimpactingplatformindependence.1)Deploymentanddistributionbecomemoreefficientwi

BytecodeinJavaistheintermediaterepresentationthatenablesplatformindependence.1)Javacodeiscompiledintobytecodestoredin.classfiles.2)TheJVMinterpretsorcompilesthisbytecodeintomachinecodeatruntime,allowingthesamebytecodetorunonanydevicewithaJVM,thusfulf


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!
