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

Thread: java

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java

    import java.io.*;

    public class locc1
    {
    int a;
    public void get()
    {
    System.out.println(a);
    }
    }


    class locsam1
    {
    public static void main(String args[])
    {
    locc1 l=new locc1();
    l.a=98;
    l.get();
    }
    }


    hai guys .. upon execution of this program it shows me an error as " class locc1 is public, should be declared in a file named locc1.java .. why do error occurs .? and i want to know the solution to get rid of this error and also i want the reason why this error is raised . please guys help me.. thanks in advance .


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: java

    Thread moved from Member Introductions

    Please read the forum rules.

    on execution of this program it shows me an error as " class locc1 is public, should be declared in a file named locc1.java .
    That pretty much says it all. A public class in a file must be within a file of the same name. Name you file as it suggests.

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

    Default Re: java

    Quote Originally Posted by vignesh gopalakrishnan View Post
    ...want the reason why...
    There are some rules that Java application programmers must live with.

    Here are some that are related to your problem, and may cover other questions that might arise early in your development.

    1. For projects consisting of one file, the file must have a public class. It can have other classes, but it can only have one public class. The file name must consist of the class name and a .java extension.

    2. Execution will start in a main function of a public class. The main() function must be declared public static void, and it must have exactly one argument, which is String array.

    3. A program can consist of material from many files. Other files can have other classes (and certain other Java constructs). Other files can have public classes. Each file can have at most one public class. If any file has a public class, the name of that file must consist of the class name and a .java extension.

    4. Of all of the files and all of the classes in a given project, only one public class can have a static void main function that has a single String array argument.


    These notes apply to Java application programs. Other types of Java programs may have somewhat different rules to live by. (Java Applets won't have a main function, for example.)

    Cheers!


    Z
    Last edited by Zaphod_b; July 25th, 2012 at 02:53 PM.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: java

    Clarifying to Zaphod's post above:
    1. Projects can have multiple public classes (even classes can have multiple public inner classes)
    3. Same dodgy language as #1, files can contain more than one public class.
    4. There can be multiple classes in a project with a main, however the JVM will only execute the main specified upon execution (this can be specified either in the IDE at runtime or in the manifest of an executable jar file)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    Default Re: java

    Quote Originally Posted by aussiemcgr View Post
    Clarifying to Zaphod's post above:...)
    Thank you for the clarification. Well, more than clarification. Thank you for the corrections.

    I'm kind of new to Java, and I "discovered" several things without reading entire books and reference manuals. I wanted to share my fresh learning experiences, and I'm sure the "rules" can be stated more precisely (and correctly).

    I will say that if I create a file with more than one public class I get a javac compiler error, and that's why I put that as part of items 1 and 2. I wasn't thinking of inner public classes.

    Maybe my list of "rules" should have been titled "Things to help you get started until you have had the time and the motivation and the stamina to read all 684 pages of Version 3 of the Java Language Specification."

    Now, getting a head start on my Winter Sports Encyclopaedia (to be published Real Soon Now), I am starting a list of "Important Rules for Beginning Skiiers."

    1. Don't eat yellow snow.



    Cheers!

    Z
    Last edited by Zaphod_b; July 25th, 2012 at 03:27 PM.