anon4u
anon4u

Reputation: 3

Can we call a function within a function in Java?

If we declare a method to be static, then without needing to instantiate we can call that method anywhere within the class body.

If we do not declare a method to be static, then an object can be instantiated and we call the method.

Now if we do not declare a method to be static and also do not instantiate, can we call a function within a function?

EDIT:

I understand now, that my hunch was right. We cannot call another function within a function unless there static or object instantiation.

But in Java applets I remember seeing a function being called from another function.

import javax.swing.*;
import java.awt.Container;

public class MethodCall extends JApplet
{
    public void init()
    {
        String output = "";
        JTextArea outputarea=new JTextArea(10,20);
        Container c = getContentPane();
        c.add(outputarea);

        int result;
        for(int x=1;x<=10;x++)
        {
            result = square(x);
            output += "Square of " + x + " is " + result + "\n";
        }//end of for loop

        outputarea.setText(output);
    }//end of init()

    public int square(int y)
    {
        return y*y;
    }//end of square()

}//end of class MethodCall

See square() function

Upvotes: 0

Views: 42200

Answers (5)

MByD
MByD

Reputation: 137312

No You can't.

If a method is not static, then it has to be called from an instance of some class. Even in the examples given here, like

public class A{

    private void foo()
    {
        bar();
    }
    private void bar()
    {

    }
}

You don't call bar just as itself, you call it on the current instance of A, which is like this.bar()

EDIT

In your example, you already have an instance of MethodCall when you are in init (otherwise you couldn't call it). so the call to square() is actually this.square(). the same for getContentPane() which is a method of JApplet (which is the super class of MethodCall)

Upvotes: 4

Amir Rachum
Amir Rachum

Reputation: 79615

Your question is not really clear, but here's a quick summary:

class A {
    public static void foo() {
        bar();      // illegal, no object
        foo();      // legal, implicit
        A.foo();    // legal, explicit
        A a = new A();
        a.bar();    // legal - we call a non-static function on an object
    }

    public void bar() {
        bar();      // legal, implicit
        this.bar(); // legal, explicit
        foo();      // legal, implicit
        A.foo();    // legal, explicit
    }
}

Note that the calls to the functions within themselves in this case are infinitely recursive.

Upvotes: 12

Hons
Hons

Reputation: 4046

That should work:

public class CallTest {

    public static void someMethod(){
        System.out.println("blabla");
    }

    public static void main(String[] args) {
        CallTest.someMethod();
    }

}

Upvotes: 0

Jeroen Baert
Jeroen Baert

Reputation: 1314

If I understand your question correctly, you're asking if you can call a function within a function in a standard non-static Java method.

That's possible, as long as the function you want to call is within the same class or class hierarchy, or the function you're calling is itself static.

Upvotes: 0

Viren Pushpanayagam
Viren Pushpanayagam

Reputation: 2497

Yes you can as long as it's visible to the function. Say for example the calling function and the called function are in the same class or a sub class. If its a sub class the called method scope should be default, protected or public.

Upvotes: 0

Related Questions