how to call methods without instantiation?

In java, is it possible to call methods from an external class without instantiating that class beforehand in the invoker class? If so, how? Thanks!

[155 byte] By [aimarthebesta] at [2007-11-27 10:12:49]
# 1

>In java, is it possible to call methods from an external class without instantiating that class

No. You can't only if the method are static

See http://java.sun.com/docs/books/tutorial/reflect/index.html

java_2006a at 2007-7-28 15:22:48 > top of Java-index,Java Essentials,New To Java...
# 2

You can call static methods of a class by using the class name to identify them - ie you do not need to dereference a reference to a particular instance of the class.

pbrockway2a at 2007-7-28 15:22:48 > top of Java-index,Java Essentials,New To Java...
# 3

what i mean exactly is:

for example:

public class A

{

.....

public int methodA()

.....

}

in another class B, do I have to write:

A instanceA = new A();

int x = A.methodA();

or is there any way i can call methodA straight away without instantiate "instanceA"?

aimarthebesta at 2007-7-28 15:22:48 > top of Java-index,Java Essentials,New To Java...
# 4

No. You must sayA instanceA = new A();

int x = instanceA.methodA();

pbrockway2a at 2007-7-28 15:22:48 > top of Java-index,Java Essentials,New To Java...
# 5

this only can be done if the class b extends the class a, then you can use syntax below,

super.mathod();

ccwoon80a at 2007-7-28 15:22:48 > top of Java-index,Java Essentials,New To Java...
# 6

sorry... typing error... should be "instanceA.methodA()"...

but no other ways? i mean call method directly...

aimarthebesta at 2007-7-28 15:22:48 > top of Java-index,Java Essentials,New To Java...
# 7

As has been said already you can call static methods without an instance.

class A {

public static void method() {...}

}

//Somewhere else

A.method();

dwga at 2007-7-28 15:22:48 > top of Java-index,Java Essentials,New To Java...
# 8

I think there is only above mentioned ways to call a method directly. In java every function and variable is written in a class. So

> sorry... typing error... should be

> "instanceA.methodA()"...

>

> but no other ways? i mean call method directly...

If you wish to access the function even without mentioning the class name, it is not possible.

jdolphina at 2007-7-28 15:22:48 > top of Java-index,Java Essentials,New To Java...
# 9

> As has been said already you can call static

> methods without an instance.

> class A {

>public static void method() {...}

>

> //Somewhere else

> A.method();

And with static imports you can do:

//Somewhere else

import static A.method;

//...

method();

-Puce

Pucea at 2007-7-28 15:22:48 > top of Java-index,Java Essentials,New To Java...
# 10

> sorry... typing error... should be

> "instanceA.methodA()"...

>

> but no other ways? i mean call method directly...

Let's try a different approach. Why do you think you need to do this? Your questions should be about your actual problem, not what you reckon will be the solution to your problem - you'll get a decent answer much quicker!

georgemca at 2007-7-28 15:22:48 > top of Java-index,Java Essentials,New To Java...