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.

Results 1 to 3 of 3

Thread: How do I call a class in a main class

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    18
    My Mood
    Nerdy
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How do I call a class in a main class

    This program I have made measures the area of the room.
    Now i want to create two classes.
    This is the original coding I made that works perfectly. But here I put everything in one class:

    import javax.swing.JOptionPane;
     
    public class RoomOne
    {
    public static void main(String[] args) throws Exception
    {
    //Declaration of strings
    String firstNumber;
    String secondNumber;
     
    int numberOne=10;
    int numberTwo=20;
    int finalAnswer=numberOne*numberTwo;
     
    JOptionPane.showMessageDialog(null, "The area of the room is: " + finalAnswer + " sqaure feet", "Room Measurement", JOptionPane.INFORMATION_MESSAGE);
    {
    System.exit(0);
    }
    }
    }

    I want to have two classes, the main class, and a secondary class.
    The secondary class should contain the entire coding. I want to be able to call the secondary class in the main class. How do I do that?
    Last edited by Leprechaun_hunter; April 16th, 2011 at 09:56 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,444
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: How do I call a class in a main class

    Your first step is to put that all in a method, then call it from the main you already have. When you have that working, then just do the same thing in another class.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Leprechaun_hunter (April 16th, 2011)

  4. #3
    Junior Member
    Join Date
    Apr 2011
    Location
    Pune, India
    Posts
    11
    My Mood
    Cool
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How do I call a class in a main class

    Hey this is initail level of codding... please read the java book.
    For your reference i am attaching the code.. But please take care....

    package com.java.main;
     
    import com.java.main.RoomTwo;
     
    public class RoomOne {
    	public static void main(String[] args) throws Exception {
    		RoomTwo r2 = new RoomTwo();
    		r2.roomDetail();
    	}
    }

    package com.java.main;
     
    import javax.swing.JOptionPane;
     
    public class RoomTwo {
    	public void roomDetail() throws Exception {
    		// Declaration of strings
    		String firstNumber;
    		String secondNumber;
     
    		int numberOne = 10;
    		int numberTwo = 20;
    		int finalAnswer = numberOne * numberTwo;
     
    		JOptionPane.showMessageDialog(null, "The area of the room is: "
    				+ finalAnswer + " sqaure feet", "Room Measurement",
    				JOptionPane.INFORMATION_MESSAGE);
    		{
    			System.exit(0);
    		}
    	}
    }
    Hardik Jadhav

Similar Threads

  1. How to call int value from another class
    By IDK12 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 14th, 2011, 08:18 PM
  2. Creating a scaleUp main method in a new class
    By Brainz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 07:58 AM
  3. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM
  4. Main Class Not Found Problem
    By shadow in forum Java Theory & Questions
    Replies: 3
    Last Post: September 29th, 2009, 09:42 AM
  5. could not find the main class
    By Tisofa in forum Object Oriented Programming
    Replies: 1
    Last Post: September 27th, 2009, 02:58 AM