Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

View RSS Feed

JD1's Personal Development Blog

Multiple Classes

Rate this Entry
In each Java application, you're going to have a series of classes containing various instructions for the compiler. Each class can be responsible for different actions or functionality of the program.

Below is our first class, appropriately named class1.

class class1{
	public static void main(String args[]){
		System.out.println("This code is executed via class1!")
	}
}

Let's say that class1 is responsible for a few introductory things, such as a welcome message and lastly referencing the other classes into action. That's often the main role of the first class, to call other classes and execute code from within them. Below is our second class.

public class class2{
	public void class2Method(){
		System.out.println("This code is executed via class2!");
	}
}

Right now we have two separate classes. We can use class1 fine, but if we want to access class2, we're going to need to say so in class1, since that is our main class containing our main method. We can do this by adding some code to class1.

class class1{
	public static void main(String args[]){
		System.out.println("This code is executed via class1!");
                            class2 class2Object=new class2();
                            class2Object.class2Method();
	}

We've added two new lines of code to class1.

class2 class2Object=new class2();
class2Object.class2Method();

The first line creates a class2 object we can use to implement methods from within that class. To create this object, you must first write the class name (in our case, it's class2). Then, you must write the name of the object you'd like to use to reference that class, it can be anything. I've kept it simple with class2Object. And lastly, write =new because we're creating a new object, followed by the name of the class once again (class2). This is shown above for your reference.

We now have a class2 object we can use to execute methods from within the second class. To actually do this, you must write the object name, followed by a dot separator, followed by the name of the method from within that class (class2Method). Your application will now execute everything from within that method of that class.
Categories
Uncategorized

Comments