Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a class method.

 
K Robert
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you're missing the 'new' keyword in the second line.
 
nik rb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K Robert
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I forgot the "new"... I didn't mean to!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic