Results 1 to 11 of 11
  1. #1
    thokchom is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Something wrong in multiple class

    Inside package called lession_2........ there are three class called Computer.java, Default_constructor.java, Defining_class.java
    And my question is when Default_constructor.java, Defining_class.java are run output gives only output of Computer.java instead of Default_constructor.java, Defining_class.java both output. I could not understand this is eclipse configure error or code error.


    below are the three classes in a package:

    Default_constructor.java
    Java Code:
    package lession_2;
    
    
    class Default_constructor {
            int a, b;
            Default_constructor()
            {
            	System.out.println("Default Constructor");
            	a = 10;
            	b = 20;
            }
            void display()
            {
            	System.out.println("value of A is: " + a);
            	System.out.println("value of B is: " + b);
            }
    
    }
    
    
    class Main1{
    public static void main(String[] args)
    {
     Default_constructor object = new Default_constructor();
     object.display();
    }
    }
    Defining_class.java
    Java Code:
    package lession_2;
    
    import java.util.Scanner;
    //this class is for defining field (variable) and method (function) 
    
    // this is for user input and accessing the above class member by using defined object
    class Defining_class {
    
        int a, b, c;
        void takendata(int x, int y)
        {
       	 a=x;
       	 b=y;
        }
        void add()
        {
       	 c=a + b;
       	 System.out.println("Addition is: " +c);
       	   
        }  
    }
    
    class Main
    {
    public static void main1(String[] args)
    {
    Scanner scan11 = new Scanner(System.in);
    System.out.println("Enter first number: ");
    int first= scan11.nextInt();
    System.out.println("Enter first number: ");
    int second= scan11.nextInt();
    Defining_class object = new Defining_class();
    object.takendata(first, second);
    object.add();
    scan11.close();
    }
    }
    And Computer.java
    Java Code:
    package lession_2;
    
    class Computer {
    	  Computer() {
    	    System.out.println("Constructor of Computer class.");
    	  }
    	 
    	  void computer_method() {
    	    System.out.println("Power gone! Shut down your PC soon...");
    	  }
    	 
    	  public static void main(String[] args) {
    	    Computer my = new Computer();
    	    Laptop your = new Laptop();
    	 
    	    my.computer_method();
    	    your.laptop_method();
    	  }
    	}
    	 
    	class Laptop {
    	  Laptop() {
    	    System.out.println("Constructor of Laptop class.");
    	  }
    	 
    	  void laptop_method() {
    	    System.out.println("99% Battery available.");
    	  }
    	}
    Last edited by JosAH; 08-22-2016 at 02:27 PM. Reason: added [code] ... [/code] tags

  2. #2
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Something wrong in multiple class

    The 'public static void main(String... args)' method is considered the run time entry point into your application. Therefore you can only run one at a time.

  3. #3
    thokchom is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Re: Something wrong in multiple class

    Quote Originally Posted by trcooke View Post
    The 'public static void main(String... args)' method is considered the run time entry point into your application. Therefore you can only run one at a time.
    hi,, thank for reply.... but I am new in this ........ so what is the solution that I can resolve this issue?


    I have tried to delete Computer.java.... no use...... and tried to run classes of lession_1 package those all are working, now I came to know that which class last time I ran that output is taking when I run Defining_class.java and Default_constructor.java
    Last edited by thokchom; 08-22-2016 at 02:56 PM.

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Something wrong in multiple class

    What are you trying todo?

  5. #5
    thokchom is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Re: Something wrong in multiple class

    Quote Originally Posted by benji2505 View Post
    What are you trying todo?
    Output console is saying:
    Error: Main method not found in class lession_3.Check, please define the main method as:
    public static void main(String[] args)
    or a JavaFX application class must extend javafx.application.Application


    When I run this code
    package lession_2;

    import java.util.Scanner;
    //this class is for defining field (variable) and method (function)

    // this is for user input and accessing the above class member by using defined object
    class Defining_class {

    int a, b, c;
    void takendata(int x, int y)
    {
    a=x;
    b=y;
    }
    void add()
    {
    c=a + b;
    System.out.println("Addition is: " +c);

    }
    }

    class User_input
    {
    public static void main(String[] args)

    {
    Scanner scan11 = new Scanner(System.in);
    System.out.println("Enter first number: ");
    int first= scan11.nextInt();
    System.out.println("Enter first number: ");
    int second= scan11.nextInt();
    Defining_class object = new Defining_class();
    object.takendata(first, second);
    object.add();
    scan11.close();
    }
    }

  6. #6
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Something wrong in multiple class

    I could not identify the "class lession_3" that is throwing the exception in any of your posts. Am I overseeing something?

  7. #7
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Something wrong in multiple class

    You sure you spelled it right? Since in defining class its public static void main1(String[] args).

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Something wrong in multiple class

    How are you attempting to run this in Eclipse?
    If you are just clicking the little green run button then all that will do is run the previously run main class.
    You need to right click on the class you want to run and select to run it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    thokchom is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Re: Something wrong in multiple class

    Thanks all of you for replied............... solved the problem from long google search,,,,,,, its due to main class User_input should be the name of as file name like User_input.java not Defining_class.java(I was saving Defining_class.java)

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Something wrong in multiple class

    Your answer is not completely correct. Consider the following:

    Java Code:
    class FirstClassWithMain{
       public static void main(String[] args) {
          System.out.println("FirstClassWithMain");
       }
    }
    
    class SecondClassWithMain {
       public static void main(String[] args) {
          System.out.println("SecondClassWithMain");
       }
    }
    
    class ThirdClassWithMain {
       public static void main(String[] args) {
          System.out.println("ThirdClassWithMain");
       }
    }
    In Eclipse, the above code will run just fine with the first, second, and third classes only contained in a file of any name.
    You simply have to create a run configuration for the desired class.

    If you put any of these classes in a file which has the same name as a class with a main entry point, then Eclipse will prompt
    you for the desired entry point.

    Regards,
    Jim
    Last edited by jim829; 08-22-2016 at 07:52 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    thokchom is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Re: Something wrong in multiple class

    Quote Originally Posted by jim829 View Post
    Your answer is not completely correct. Consider the following:

    Java Code:
    class FirstClassWithMain{
       public static void main(String[] args) {
          System.out.println("FirstClassWithMain");
       }
    }
    
    class SecondClassWithMain {
       public static void main(String[] args) {
          System.out.println("SecondClassWithMain");
       }
    }
    
    class ThirdClassWithMain {
       public static void main(String[] args) {
          System.out.println("ThirdClassWithMain");
       }
    }
    In Eclipse, the above code will run just fine with the first, second, and third classes only contained in a file of any name.
    You simply have to create a run configuration for the desired class.

    If you put any of these classes in a file which has the same name as a class with a main entry point, then Eclipse will prompt
    you for the desired entry point.

    Regards,
    Jim

    Yes you are right the given ur code is simple class........ that's why it is working....... according to which I found in google... If I call the instance members of class by creating object then where the object I have created in a class which classs name should be the name of java class file like User_input.java


    package lession_2;

    import java.util.Scanner;
    class User_input
    {
    public static void main(String[] args)

    {
    Scanner scan11 = new Scanner(System.in);
    System.out.println("Enter first number: ");
    int first= scan11.nextInt();
    System.out.println("Enter first number: ");
    int second= scan11.nextInt();
    // creating object to assign the input value
    Defining_class object = new Defining_class ();
    object.takendata(first, second);
    object.add();
    scan11.close();
    }
    }

    class Defining_class {
    // instance members
    int a, b, c;
    void takendata(int x, int y)
    {
    a=x;
    b=y;
    }
    void add()
    {
    c=a + b;
    System.out.println("Addition is: " +c);

    }
    }



    Thanks a lot

Similar Threads

  1. What is wrong with my driver and or main class
    By Sammie in forum New To Java
    Replies: 3
    Last Post: 12-01-2014, 12:35 AM
  2. Main class ~Multiple class
    By aquilina in forum New To Java
    Replies: 1
    Last Post: 12-12-2013, 02:47 PM
  3. Replies: 4
    Last Post: 02-16-2011, 04:31 AM
  4. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 12:10 PM
  5. whats wrong with this class declaration?
    By blossompark in forum New To Java
    Replies: 5
    Last Post: 11-30-2008, 02:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •