How to use several separate classes (separate files) to be executed in one class (another file)

E

EvgueniB

Hi,
Looking for an example, where couple separate classes in separate files will
be attached and used (executed) in new class in separate file.

Thanks in advance!!!
 
A

Anthony Borla

EvgueniB said:
Hi,

Looking for an example, where couple separate classes in
separate files will be attached and used (executed) in new
class in separate file.

// *** Separate Package ***

javac xx/A.java
javac xy/B.java
javac xz/UseAndB.java
java xz.UseAndB

// File: A.java
package xx;
public class A { public void doA() {}}

// File: B.java
package xy;
public class B { public void doB() {}}

// File: UseAandB.java
package xz;
import xx.A;
import xy.B;

public class UseAandB
{
public static void main(String[] args)
{
A a = new A(); a.doA();
B b = new B(); a.doB();
}
}

// *** Same Package ***

javac xp/UseAndB.java
java xp.UseAndB

// File: A.java
package xp;
public class A { public void doA() {}}

// File: B.java
package xp;
public class B { public void doB() {}}

// File: UseAandB.java
package xp;
public class UseAandB
{
public static void main(String[] args)
{
A a = new A(); a.doA();
B b = new B(); b.doB();
}
}

I hope this helps.

Anthony Borla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top