How to create a java class
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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
