Home  >  Article  >  Java  >  What are the declaration syntaxes for classes in java

What are the declaration syntaxes for classes in java

下次还敢
下次还敢Original
2024-04-29 02:00:27769browse

The declaration syntax of a class in Java is: use the class keyword to declare a class. The first letter of the class name is capitalized, and subsequent letters can be uppercase or lowercase. Member variables consist of data types and variable names. The first letter of the variable name is lowercase. Member methods consist of return type, method name and parameter list. The first letter of the method name is lowercase.

What are the declaration syntaxes for classes in java

Declaration syntax for classes in Java

In Java, use the class keyword To declare a class, the syntax format is as follows:

<code class="java">public class <ClassName> {
  // 成员变量
  // 成员方法
}</code>
  • public: access modifier, indicating that the class is public and can be accessed by all classes.
  • class: Keyword, indicating declaring a class.
  • <ClassName>: Class name must start with an uppercase letter, and subsequent letters can be uppercase or lowercase.
  • Cure braces: Contains member variables and member methods of the class.

Member variables

Member variables are fields that store data in the class. The syntax format is as follows:

<code class="java"><DataType> <VariableName>;</code>
  • <DataType>: The type of data, such as int, double, String, etc.
  • <VariableName>: Variable name must start with a lowercase letter, and subsequent letters can be uppercase or lowercase.

Member method

Member method is the behavior defined in the class. The syntax format is as follows:

<code class="java"><ReturnType> <MethodName>(<ParameterList>) {
  // 方法体
}</code>
  • <ReturnType>: The return type of the method, if the method does not return any value, it will be void.
  • <MethodName>: The method name must start with a lowercase letter, and subsequent letters can be uppercase or lowercase.
  • <ParameterList>: Method parameter list, if the method has no parameters, it will be empty.
  • Cure braces: Contains the method body and defines the behavior of the method.

The above is the detailed content of What are the declaration syntaxes for classes in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn