Static and non-static method in one class with the same name JAVA

milo

I know it is impossible to override a method in one class. But is there a way to use a non-static method as static? For example I have a method which is adding numbers. I want this method to be usefull with an object and also without it. Is it possible to do something like that without creating another method?

EDIT: What I mean is, if I make a method static I will need it to take arguments, and if I create an object with variables already set it will be very uncomfortable to call function on my object with same arguments again.

public class Test {

    private int a;
    private int b;
    private int c;

    public Test(int a,int b,int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public static String count(int a1,int b1, int c1)
    {        
        String solution;
        solution = Integer.toString(a1+b1+c1);
        return solution;
    }


    public static void main(String[] args) {

       System.out.println(Test.count(1,2,3));
       Test t1 = new Test(1,2,3);
       t1.count();
    }

}

I know the code is incorrect but i wanted to show what I want to do.

Helder Pereira

I want this method to be usefull with an object and also without it. Is it possible to do something like that without creating another method?

You will have to create another method, but you can make the non-static method call the static method, so that you do not duplicate the code and if you want to change the logic in the future you only need to do it in one place.

public class Test {
    private int a;
    private int b;
    private int c;

    public Test(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public String count() {
        return count(a, b, c);
    }

    public static String count(int a1, int b1, int c1) {
        String solution;
        solution = Integer.toString(a1 + b1 + c1);
        return solution;
    }

    public static void main(String[] args) {
        System.out.println(Test.count(1, 2, 3));
        Test t1 = new Test(1, 2, 3);
        System.out.println(t1.count());
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Static and non static methods with the same name in parent class and implementing interface

From Dev

Java static and non-static variables in the same class

From Java

Getting the class name from a static method in Java

From Dev

How to use a static and an instance method with the same name on the same class?

From Java

Java no name static method

From Dev

How to call static method of a class that has a function with the same name as this class?

From Dev

How does java's double colon operator refer non-static method using class name

From

How to call non-static method from static method of same class?

From Dev

May static and non static variables in class have the same name according to DartLangSpec-v2.2

From Java

Getting the class name from a generic static method in Java

From Dev

Call non static method from different non static class

From Dev

static inner class and static member of a class shares SAME NAME?

From Dev

calling a java constructor of same class from non static context leads to recursion but with static it works fine?

From Dev

PHP call a static method of a class by class name

From Dev

May static and non static variables have the same name

From Java

What happens when a Static Variable has the same name as a Static Class in Java?

From Java

Is it wrong to have static and non-static methods in the same class?

From Dev

How to enter a non static method from a static one

From Dev

How to have Static and Method Functions with the same name

From Dev

java 8 method reference calling non static method in a static way

From Java

Java 8: method reference to a static method in a non-static way

From Java

calling non-static method in static method in Java

From Dev

Call non-static method in Static method JAVA

From Dev

Anonymous method in static class is non-static? How to invoke it?

From Java

static vs non-static method for immutable class

From Dev

Implementing static and non-static method of a class performance

From Java

How to instantiate non static inner class within a static method?

From Java

performance of static vs non static method for an utility class

From Dev

unit test for private static method in non-static class

Related Related

  1. 1

    Static and non static methods with the same name in parent class and implementing interface

  2. 2

    Java static and non-static variables in the same class

  3. 3

    Getting the class name from a static method in Java

  4. 4

    How to use a static and an instance method with the same name on the same class?

  5. 5

    Java no name static method

  6. 6

    How to call static method of a class that has a function with the same name as this class?

  7. 7

    How does java's double colon operator refer non-static method using class name

  8. 8

    How to call non-static method from static method of same class?

  9. 9

    May static and non static variables in class have the same name according to DartLangSpec-v2.2

  10. 10

    Getting the class name from a generic static method in Java

  11. 11

    Call non static method from different non static class

  12. 12

    static inner class and static member of a class shares SAME NAME?

  13. 13

    calling a java constructor of same class from non static context leads to recursion but with static it works fine?

  14. 14

    PHP call a static method of a class by class name

  15. 15

    May static and non static variables have the same name

  16. 16

    What happens when a Static Variable has the same name as a Static Class in Java?

  17. 17

    Is it wrong to have static and non-static methods in the same class?

  18. 18

    How to enter a non static method from a static one

  19. 19

    How to have Static and Method Functions with the same name

  20. 20

    java 8 method reference calling non static method in a static way

  21. 21

    Java 8: method reference to a static method in a non-static way

  22. 22

    calling non-static method in static method in Java

  23. 23

    Call non-static method in Static method JAVA

  24. 24

    Anonymous method in static class is non-static? How to invoke it?

  25. 25

    static vs non-static method for immutable class

  26. 26

    Implementing static and non-static method of a class performance

  27. 27

    How to instantiate non static inner class within a static method?

  28. 28

    performance of static vs non static method for an utility class

  29. 29

    unit test for private static method in non-static class

HotTag

Archive