How to use class methods [Java]

Introduction

Here are some things to keep in mind when using class methods with instance variables and method methods.

What is a class method?

Class methods are primarily responsible for accessing class variables. You must add the static qualifier when declaring it. When calling, use the class name as follows.

(name of the class).(Class method name)

Relationship between class methods and other variables and methods

The following description is quoted from Oracle's The Java TM </ sup> Tutorials.

  • Instance methods have direct access to class variables and class methods
  • Class methods have direct access to class variables and class methods
  • Class methods do not have direct access to instance variables and methods ***. You need to refer to the object to access it. Also, the this keyword cannot be used because there is no object indicated by this keyword.

The above "directly accessible" means that you can call only the variable name or only the method name in the processing inside the method.

Concrete example

Here is an example of calling an instance method in a class method.

The class method to be used is main, and the instance method is display. main directly accesses the string type class variable and displays the contents of the string on the console. display displays an integer argument on the console.

Test.java


public class Test{

	public static String str = "The class variable was called"; //Class variables

	public static void main(String[] args){ //Class method
	
		System.out.println(str); //Direct access to class variables
		
		Test test = new Test(); //Generate Test type object
		
		test.display(5); 		//Browse Test type object to access instance method
		
	}
	
	public void display(int num){ //Instance method
		System.out.println("The numbers are" + num + "is");
	}

}

When you run Test.java

The class variable was called
The number is 5

have become.

main is a class method because it has a static modifier. display is an instance method because it doesn't have a static modifier. Therefore, to access display from main, you need to refer to the Test type object. It feels strange to need an object reference when using methods of the same class ...

in conclusion

In this article, we've looked at mutual access between class and instance methods, rather than how to use the class methods themselves.

As a result, I had to be a little careful when calling an instance method inside a class method within the same class.

Then I introduced an example of simple code that directly accesses a class variable within a class method and calls an instance method.

Recommended Posts

How to use class methods [Java]
How to use java class
[Java] How to use LinkedHashMap class
[Java] How to use Math class
[Java] How to use the File class
[Java] How to use the HashMap class
How to use Java Scanner class (Note)
[Java] How to use the Calendar class
[Java] How to use Map
[Java] How to use Map
How to use java Optional
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
[Java] How to use FileReader class and BufferedReader class
[Java] How to use Calendar class and Date class
[Java] How to use compareTo method of Date class
How to use Java HttpClient (Get)
Java class methods
How to use Java HttpClient (Post)
[Java] How to use join method
How to use the wrapper class
[Processing × Java] How to use variables
How to decompile java class files
[JavaFX] [Java8] How to use GridPane
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
How to use Java enum type
Multilingual Locale in Java How to use Locale
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
How to use substring and substr methods
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use java.util.logging
How to use map
How to use Twitter4J
How to use active_hash! !!
How to use hidden_field_tag
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5