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

Thread: Top-level class

  1. #1
    Junior Member
    Join Date
    Dec 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Top-level class

    Hello, i'm beginning with java and i'm confused about running/compiling program.
    I have A.java file with following code:

    public class B {
        public static void main(String... args) {
            System.out.println("B class");
        }
    }
     
     class C {
        public static void main(String... args) {
            System.out.println("C class");
        } 
    }

    I read that i can use feature single-file-source-code, so i don't have to write "javac A.java" and then "java A" for running my simple programs. But when i run it with "java A.java" i get no error. If i compile it with javac, then i get error (A.java:1: error: class B is public, should be declared in a file named B.java) as i would expect.

    Can anyone explain to me, why is it working differently? Thanks

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,168
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Top-level class

    See the responses here: https://coderanch.com/t/787353/java/Top-level-class
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: November 22nd, 2018, 10:12 AM
  2. Replies: 0
    Last Post: October 14th, 2013, 08:30 PM
  3. Class level Lock vs Object level lock
    By hs82 in forum Java Theory & Questions
    Replies: 0
    Last Post: October 14th, 2013, 10:17 AM
  4. how to maintain a unpopulated class -newbie level
    By wuwu_sister in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 16th, 2013, 02:43 PM
  5. how to set up level class in 2d java game
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2012, 06:17 PM