Java programming (class structure)

Class concept

Here's an overview of the classes that Java cannot avoid. You can read it if you know variables and simple calculations. On the contrary, deep things are not introduced in this article. Maybe it's rare to write in this way?

What is a class

Classes make Java, leaving the exact definition of classes to other articles and books. In fact, various Java features are defined in classes. And by creating your own class, you can create the functions you want.

Class structure

Now let's actually configure the class. Here, let's create a Test class. Classes have processes called variables and methods. Let's make a Test class in Japanese.

public class Test{
/*
 *Declare class variables here and there
 */
/*
 *Here, the processing that you want to realize in the class'Method'Define
 */
}

Class variables

The variables of the class are written as follows.

private int a = 4;

Here, 4 is put in an integer type variable called a. Variables often use the following two naming rules:

Descriptive name

Write the name of the variable. Although it is not a Java rule, the variable name must be something that represents the content of the process or operation. Give it a name that others can easily see. LowerCamel Also, in general, it tends to be attached by connecting English words, but uppercase and lowercase letters are generally fixed. Generally, the first English word is written in lowercase, and the second and subsequent letters are written in uppercase.

Example. When creating a method name with two English words Java Qiita for variable names

Correct example   javaQiita, qiitaJava

bad example   javaqiita, JavaQiita

By the way, private is explained together in the method description.

Class method

Next is the method. The method describes such processing and the operation of the variable you want to perform. And finally, it returns the result of processing and operation. The method has the following structure.

public Return type Method name(Argument type Argument){
/*
*The contents of the method
*Write the process or operation you want to achieve with the method.
*/

return Return value;
}

Let's explain one by one

Return value, return type

The result returned by the method is called the return value. And the method first writes what type of value the return value is. Value at the end of processing

return Return value;

Write. By the way, if the method executes this return, it will end there.

Method name

Method names have the same naming rules as variables.

Argument, argument type

The method may also contain some value. For example, the process of multiplying a certain number by 3 or the process of adding 4 to a certain number. This certain number is entered as an argument. Arguments use the same naming convention as variables.

Access specifier

Now let's touch on the access specifier. The access specifier is private or public of the method or class that appears when the variable is used. This refers to the range that can be used for that variable or method. public You can use this method or variable from other classes. private You can use methods and variables only from within your class. For beginners, it's safe to remember that classes and methods are public and variables are private.

Class example

For example, create a method that triples the argument and returns it.

public int multipleIntegerNumber(int num){
 num = 3*num;
 return num;  
}

Summary

Here, I introduced how to write the contents of the class. I will explain how to actually use the class in the next article. Classes and instantiations (please wait a moment) Also related to access specifiers Encapsulation and setter, getter (please wait a moment)

Recommended Posts

Java programming (class structure)
Java programming (class method)
Java class methods
java Scanner class
Java HashMap class
java programming basics
java (abstract class)
[Java] Nested class
Java anonymous class
[Java] Basic structure
java Generic Programming
About Java class
[java] abstract class
[Java] Object class
Java local class
[Java] Inheritance and structure of HttpServlet class
Java programming (static clauses and "class variables")
Constraint programming in Java
About class division (Java)
About Java StringBuilder class
[Java] About Singleton Class
Java inner class review
Java programming basics practice-array
Java class type field
About Java String class
All about Java programming
Java tree structure library
java competitive programming memo
About java abstract class
Java Programming Thread Runnable
[Java] Integer wrapper class reference
Java memo (standard class) substring
Java tips --StaticUtility class modifier
Amazing Java programming (let's stop)
Java inflexible String class substring
Java memo (standard class) length
Java programming (variables and data)
[Implementation] Java Process class notes
About Java class loader types
How to use java class
Java Development Basics-Practice ③ Advanced Programming-
[Java] Comparator of Collection class
Java class definition and instantiation
Java programming basics practice-for statement
Java learning memo (abstract class)
Summary of Java Math class
Java programming basics practice-switch statement
[Java] What is class inheritance?
About Java class variables class methods
[Java basics] What is Class?
List data structure [Java / Scala]
[Java] Basic terms in programming
[Java] Set structure of collection class (about HashSet and TreeSet)
Why are class variables needed? [Java]
[Java] Object-oriented syntax --class method / argument
Various methods of Java String class
[Java] Memo for naming class names
Competitive programming private cheat sheet (Java)
How to disassemble Java class files
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
Introduction to Functional Programming (Java, Javascript)