user3006216
user3006216

Reputation: 43

Can I call a non static method from a static method?

I have shown the full class here in case there is something i need to specifically change. I am wanting to call the "power" method in the main method as you can see in the code but it will not work because one of the methods are static and the other one is not. Doe anyone know of a way around this problem? All help appreciated!

public class Power
{
    public  int square(int x){
        return x*x;
    }

    public int cube(int x){
        return power(x, 3);
    }

    public int hypercube(int x){
        return power(x, 4);
    }

    public  int power(int x, int n)
    {
        if (n==1)
           System.out.println(n);

        if (n==2)
            square(x);

        if (n==3)
            cube(x);

        if (n==4)
            hypercube(x);
    }

    public static void main(String[] args)
    {
        int x = 6;

        Power p = new Power();

        System.out.println( "The square of " + x + " is: " + power( x, 2 ) );

        System.out.println( "The cube of " + x + " is: " + power( x, 3 ) );

        System.out.println( "The hypercube of " + x +" is: " + power(x, 4));
    }
}

edit: I have made the changes but i am getting a Stackoverflow error on lines 15, 26 and 34. here is the changed code.

public class Power{

    public static int square(int x){
        return x*x;
    }


    public static  int cube(int x){
        return power(x, 3);
    }

    public static int hypercube(int x){
        return power(x, 4);
    }




    public static int power(int x, int n){
        if (n==1){
            System.out.println(n);
        }

        if (n==2){
            square(x);
        }
        if (n==3){
            cube(x);
        }
        if (n==4){
            hypercube(x);
        }
        return x;


    }

    public static void main(String[] args){
        int x = 6;

        System.out.println( "The square of " + x +
                " is: " + power( x, 2 ) );

        System.out.println( "The cube of " + x +
                " is: " + power( x, 3 ) );

        System.out.println( "The hypercube of " + x +
                " is: " + power( x, 4 ) );
    }    
}

line 15 is:

return power(x, 3);

line 26 is:

if (n==1){

line 34 is:

cube(x);

Upvotes: 2

Views: 509

Answers (4)

mani kandan
mani kandan

Reputation: 1

Yes you can.

See the code below as an example:

class Cal {
    int cube(int x) {
        return x * x * x;
    }
    public static void main(String args[]) {
        //int result=Cal.cube(6);
        //Cal q=new cal();
        //q.cube(6);
        System.out.println(Cal.cube(6));
    }
}

Upvotes: 0

user2085599
user2085599

Reputation:

You can't call a non-static method from a static one. The main reason is that a non-static method does not exist unless an object has been created for it already. The static method can be called without having an instance of that class.

Quick Explanation of Static Methods in Java

In-depth Explanation


Fixing all the Problems in Your code

Your power() method does not return anything.

You have some weird-recursive-problem in your method calls. You need to check how you are delegating the cube() and hypercube() logic.


public class Power
{
   public static int square(int x)
   {
      return x*x;
   }

   public static int cube(int x)
   {
      return x*x*x;
   }

   public static int hypercube(int x)
   {
      return x*x*x*x;
   }

   public static int power(int x, int n)
   {
      if (n==2)
        return square(x);

      if(n==3)
        return cube(x);

      if(n==4)
        return hypercube(x);

      return x;
   }

   public static void main(String[] args)
   {
      int x = 6;

      System.out.println( "The square of " + x + " is: " + power(x, 2));

      System.out.println( "The cube of " + x + " is: " + power(x, 3));

      System.out.println( "The hypercube of " + x + " is: " + power(x, 4));
   }
}

Learning task for you!

This will work, however, you can definitively improve this code further. Think about this: how can you reuse the square() method to calculate cube and hypercube.

Upvotes: 3

buzzsawddog
buzzsawddog

Reputation: 662

It depends on your requirements...

In the case you presented it looks more like you need static methods rather than dealing with an Instance of Power.

As one of the other answers states, if your not accessing any instance variables you should set your methods to static.

For example:

public static int power(int x, int n)

Now from main you can do:

System.out.println( "The square of " + x + " is: " + Power.power( x, 2 ) );

and do away with Power p = new Power();

If indeed you are being asked to use a Power object as an Instance.

It will be as simple as:

System.out.println("The square of " + x + " is: " + p.power(x, 2));

If you are trying to write your own math functions. STOP and use java.lang.Math More info can be found here.

Upvotes: 0

fabian
fabian

Reputation: 82461

Make all the functions static.

Tip: All functions that do not access non-static fields or functions of the class can be made static.

In this case the functions only use their parameters and can therefore be made static.

Upvotes: 0

Related Questions