Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:45:53+00:00 2026-06-04T20:45:53+00:00

I am new to Java programming, i created one class with name as Demo

  • 0

I am new to Java programming, i created one class with name as Demo and saved as sample.java while compile this java program it giving compilation error as below

  public class Demo {
        public static void main(String[] args) {
        System.out.println("This is year");
                }
            }

sample.java:3: class Demo is public, should be declared in a file named Demo .java

why do we declare public classes in individual files……

Thanks

Mukthyar

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:45:55+00:00Added an answer on June 4, 2026 at 8:45 pm

    @Oli Charlesworth is right: this is required by Java language specification. You can probably ask why do they require this? The possible answer is to make things clear. Class is atomic unit by definition. You can replace class by its other version and if the public interface was not changed everything will continue working.

    So, Java language designers decided to force us to store each classes in separate files.

    They just did some exception for non-public classes. Several non-public classes can be saved in one file. I think that the reason is that non-public class is a kind of gory details of the implementation that can be changed at any time by author. Since class is non-public these changes should not affect any API user.

    But I strongly recommend you to save every class (even if it is not public) in separate file. This improves code readability and in the end of the day its quality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before I start know this: I am extremely new to Java and programming. How
I am new to Android and Java programming. I have a class which implements
I'm very new to Java programming language so this is probably dumb question but
Is it possible to somehow 'import' a new Java class into a running program
i'm new to Java programming, but I've done C++ for a while now. the
I have been programming Java for around 15 years. While this is a basic
I'm new to Android Java programming. I've created a simple chat app. Things are
hi I am new here and new in Java programming as well actually my
sry about my english :) Im new to Java programming and i have a
First, i'm new at Java-programming and my native lang is not english, but still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.