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

invoking classes

Adrienne Peck
Greenhorn

Joined: Jul 10, 2006
Posts: 29
i'm working with a class (class a) that has code with constructors and methods (code x). these same constructors and methods are in another class (class b). i was told to invoke class b so that class invokes class b instead of running code x.

my cobol days taught me how to call one program from another program. i'm not sure of the syntax to invoke one class from another class.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

I'm sure you've done it already: for example,

System.out.println("hi");

Here, "System.out" is a static member variable -- an instance of class java.io.PrintStream -- and "println" is a method in that class.

So all you need is an instance of the other class, and then you can call its methods. I don't know the details of your specific classes, of course, but generally, this might look something like (in class "a"):

b myB = new b();
myB.x();


[Jess in Action][AskingGoodQuestions]
Adrienne Peck
Greenhorn

Joined: Jul 10, 2006
Posts: 29
Thanks!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

Note that you don't "invoke a class", you invoke methods on an instance of a class (= on an object).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
wood burning stoves
 
subject: invoking classes