About Java static and non-static methods

I'm going to touch Java, so don't forget it (if you make a mistake, I'd appreciate it if you could comment) If you are not familiar with static and non-static methods, just for reference ...

If you are a beginner in the first place, why not add static to the method as a magic trick? I didn't understand the meaning and wrote it for the time being! Lol First look at the two codes below.

-Static and non-static methods

1. static method

A.java


class A {
    public static void main(String[] args){
        calc(); //Call the calc method
    }
    
    static void calc(){ //calc is a static method
        //Calculation process
    }
}

2. Non-static method

A.java


class A {
    public static void main(String[] args){
        calc(); //Call the calc method
    }
    
    void calc(){ //calc is a non-static method
        //Calculation process
    }
}

Both programs are trying to call the calc method from the main method. However, programs that use the wrong static will be compiled when compiled. "Non-static methods cannot be referenced from static contexts" I get the error.

Why do I get such an error? ** static method (main) cannot directly access non-static method (calc) in the same class (A) </ font> ** Because it is supposed to be.

Now let's see how you can call methods in the same class.

-How to call a method

There are two main ways to call a method in a class.

1. Add static to the method and execute it (static method)

This is as described in [1. static method] above. If you add static to a method, it will be a shared method within the class, so it can be accessed from other methods.

2. Create an instance of the class and execute it through the instance (non-static method)

It's difficult to understand in words, so I'll show it in code as well.

A.java


class A {
    public static void main(String[] args){
        A a = new A(); //Create an instance called a
        a.calc(); //Call the calc method on instance a
    }
    
    void calc(){ //calc is a non-static method
        //Calculation process
    }
}

Isn't it not possible to access non-static methods in the same class from static methods? Some people may have thought. This method calls the ** calc method through ** instance a by creating an instance a of class A in the main method. Therefore, you are not directly accessing it, but you are accessing ** indirectly **, so you can call it.

・ At the end

It would be great if you could somehow understand static and non-static methods. Perhaps the reason for Java beginners to write static when writing a method is to prevent an error when called from the main method! Lol static may be attached to a variable. I will write an article about static variables next time.

Thank you for reading for me until the end. Natsu.

Recommended Posts

About Java static and non-static methods
Check static and public behavior in Java methods
[Java] Stack area and static area
[Java] Generics classes and generics methods
[Java] About String and StringBuilder
About pluck and ids methods
Java methods and method overloads
About Java class variables class methods
About Java Packages and imports
Java abstract methods and classes
Java methods
Java static
About fastqc of Biocontainers and Java
About the equals () and hashcode () methods
[Java beginner] About abstraction and interface
Java methods
About static
Studying Java 8 (StaticIF and Default methods)
Java generics (defines classes and methods)
Think about the differences between functions and methods (in Java)
About Java primitive types and reference types
This and that about Base64 (Java)
JAVA learning history final modifier and static modifier
Java programming (classes and instances, main methods)
[About JDBC that connects Java and SQL]
JAVA learning history abstract classes and methods
Java programming (static clauses and "class variables")
Find out about instance methods and self
About Java interface
Java class methods
[Java] About Java 12 features
Java static story
[Java] About arrays
About singular methods
Something about java
Where about java
About HttpServlet () methods
About Java features
Functions and methods
About Java threads
[Java] About interface
About Java class
Java and JavaScript
About Java arrays
XXE and Java
About java inheritance
About interface, java interface
About List [Java]
About java var
About Ruby methods
About Java literals
About Java commands
[Java] Personal summary of classes and methods (basic)
About Java data types (especially primitive types) and literals
[Swift vs Java] Let's understand static and final
How to access Java Private methods and fields
About Java log output
About Java functional interface
Java, about 2D arrays
About class division (Java)
About [Java] [StreamAPI] allMatch ()