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 4 of 4

Thread: Is is possible multiple public class in one java file? help needed..urgent

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Location
    Dhaka
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Is is possible multiple public class in one java file? help needed..urgent

    I am a new java learner .I just copied these following code from a book and put to the compiler ,there are 2 public classes....the compiler says "The public type RunBank must be defined in its own file".!!! I don't understand whats wrong with it? Is the book wrong?
    why the code is not working? help me..plzzzzz



     public class BankAccount 
    { 
         public float balance = 0.0F; 
    } 
    public class RunBank 
     { 
          public static void main( String args[] ) 
       { 
                System.out.println( "Start main " ); 
                BankAccount richStudent = new BankAccount( ); 
                richStudent.balance = (float) 100000; 
                BankAccount poorInstructor = new BankAccount( ); 
                poorInstructor.balance = 5.10F; 
                System.out.println( "Student Balance: " + 
                richStudent.balance ); 
               System.out.println( "Prof Balance: " + poorInstructor.balance ); 
        } 
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Is is possible multiple public class in one java file? help needed..urgent

    There can be only one top-level public class per .java file. You can split the two classes into separate .java files in the same package OR leave them as is but remove the 'public' from in front of the BankAccount class.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Location
    Dhaka
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is is possible multiple public class in one java file? help needed..urgent

    hi, Thnx for your reply,
    if I remove the 'public ' ,then the compiler says : " Error: Main method not found in class BankAccount, please define the main method as:
    public static void main(String[] args)".
    Would you please edit and give the code.... I just want to do this whole code in one file.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Is is possible multiple public class in one java file? help needed..urgent

    Here's another rule I didn't mention but that should be somewhere in that book you're reading. (You should read more carefully, thoroughly, or both.)

    - In order for a Java file to compile to executable code, the Java file that contains the top-level public class with the main() method has to have the same name as the top-level public class with the extension ".java." For example, if the top-level public class that contains the main() method is named "Name," then the file that contains the class must be named "Name.java" in order to be executable once compiled.

Similar Threads

  1. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 02:30 PM
  2. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  3. What's difference between public class and abstract class?
    By Java95 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 24th, 2012, 07:37 AM
  4. beginner-urgent java help needed
    By joeyyusiyi22 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2011, 10:43 AM
  5. Urgent Help needed with java codes
    By makarov in forum Java Theory & Questions
    Replies: 0
    Last Post: November 13th, 2009, 07:23 AM