• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling a class method.

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like you're missing the 'new' keyword in the second line.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so I forgot the "new"... I didn't mean to!
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic