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

Thread: One Simple Question

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default One Simple Question

    Testing
    Last edited by Shivafeb17; February 1st, 2018 at 07:13 AM.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: One Simple Question

    Quote Originally Posted by Shivafeb17 View Post
    If I have class1, class2 and class3...Why cannot I use the file name of the class that contains the main()?
    You can compile any blasted, bloomin' thing you want.

    However to execute a Java application program, you need a public class with a public static void main(String [] args){} method. Like it or not, that is the rule.

    To start execution with that main() method in that class, the file must be named according to the name of that public class (with a .java extension).

    Reference: Java Tutorial Essentials, Part 1, Lesson 1: Compiling and Running a Simple Program

    Still puzzled?

    Then create a file with whatever classes and methods you want. Give the file whatever name suits your fancy.

    Try to compile it.

    If it compiles successfully, try to execute something in it.

    If it doesn't work, look at the compiler error message(s) and if you still don't get it, show the exact, entire code and show the exact, complete message(s) and ask about whatever you don't understand.



    Cheers!

    Z

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: One Simple Question

    ...
    Last edited by Shivafeb17; February 1st, 2018 at 07:13 AM.

  4. #4
    Member Cronus's Avatar
    Join Date
    Feb 2013
    Location
    Gothenburg, Sweden
    Posts
    41
    My Mood
    Inspired
    Thanks
    6
    Thanked 7 Times in 6 Posts

    Default Re: One Simple Question

    Correct me if I'm wrong, do you want to know why
    public static void main(String Args[]) { }
    is needed?

    As Zaphod_b said:
    You can compile any blasted, bloomin' thing you want.
    But in order to actually execute something at all, you'll have to include a main class somewhere. This is because Java executes the lines within the main first and foremost. Without it, stupid Java won't know where to start.

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: One Simple Question

    ....
    Last edited by Shivafeb17; February 1st, 2018 at 07:13 AM.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: One Simple Question

    I'm not sure what your problem or misunderstanding is. But to help keep things tidy you should place each class (except inner classes) in their own file. So class Fred is saved in Fred.java, class Barney is saved in Barney.java etc. It makes no difference what order you write or save files. When it comes to compiling you can: javac *.java and all files will be compiled or simply compile the class that contains the main method and all other classes will be compiled as needed.
    Improving the world one idiot at a time!

Similar Threads

  1. Simple question
    By Senovit in forum Java Theory & Questions
    Replies: 2
    Last Post: September 16th, 2012, 07:09 PM
  2. One simple question...
    By chrish in forum Java Theory & Questions
    Replies: 15
    Last Post: June 27th, 2012, 11:15 AM
  3. Hi, i have a simple question.
    By oror84 in forum Java Theory & Questions
    Replies: 3
    Last Post: April 26th, 2012, 03:39 PM
  4. Simple I/O Question...well could be simple for you?
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 30th, 2011, 06:44 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM