File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

invoking classes

Adrienne Peck

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

Joined: Jul 08, 2003
Posts: 24199


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


Here, "System.out" is a static member variable -- an instance of class -- 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();

[Jess in Action][AskingGoodQuestions]
Adrienne Peck

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

Joined: Aug 16, 2005
Posts: 15081

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
I agree. Here's the link:
subject: invoking classes
It's not a secret anymore!