aspose file tools*
The moose likes Beginning Java and the fly likes Calling a class method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling a class method." Watch "Calling a class method." New topic
Author

Calling a class method.

K Robert
Ranch Hand

Joined: May 16, 2003
Posts: 116
If I have 2 classes: A & B. Class A wants to call a method from B, is the following correct?

Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Looks like you're missing the 'new' keyword in the second line.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
nik rb
Greenhorn

Joined: Sep 25, 2005
Posts: 21
ken dear u disappoint me!!! heres your code
code:
----------------------------------------------------------------------------

public void methodInA() {
methodInB b = methodInB();
System.out.println(b.coolMethod());
}

---------------------------------------------------------------------------


--------------------------------------------------------------------------------


try using
code:
----------------------------------------------------------------------------

public void methodInA() {
methodInB b = new methodInB();//u mised new..
System.out.println(b.coolMethod());}

----------------------------------------------------------------------------


----------------------------------------------------------------------------


u must be "new" to java
wont wory we've all been there
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think this is going to a confused place. methodInB starting with a lower case "m" ought to be a method, not a class. From there we can only get more confused. So let's back up:

Now we can see that B is a class and someMethod is a method in B. When A wants to call someMethod in B it has to have a reference to an instance of B. So A does a "new B()" to create an instance and saves the reference in the variable "test". Then it can call the method.

Static methods are an exception to this rule, but let's wait a day to go there.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
K Robert
Ranch Hand

Joined: May 16, 2003
Posts: 116
Okay, so I forgot the "new"... I didn't mean to!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
As I always said when teaching 5th grade band, any day we get to the end together is a good day!
S Thanigaivel
Ranch Hand

Joined: Oct 06, 2005
Posts: 60
Topic is calling a class method

but we aren't try to call a class method here

A 'class method ' is a static method in some class.

In the above examples Class B doesn't contain a class method

following code is more appropriate

public class A
{
public static void main(String[] args)
{
String result = B.someMethod();
System.out.println(result);
}
}

class B
{
// class (or static) method
public static String someMethod()
{
return "Hello";
}
}

Check this out
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a class method.