Declaring a class..........

Hi all,
Can you please tell me , in a file why it is not allowed to declare more than one class as public and also the file name should be the name of the public class?
Thanks in advance,
Regards,
Devi

1) the compiler forces you to do so.
2) it would be a configuration nightmare if it were not so
3) every class defined in a source file corresponds to a class file
4) if you had a compiler error, or an exception, or a classpath
problem, then where would you look for the source code involved?
5) it's true that the class file contains a constants pool and line number
table and the source file name is in there. but why make things so
complicated when it's easy to have the source file and class file the same
name?

Similar Messages

  • What are the uses of declaring a class "final"?

    Can any one please tell me the uses of declaring a class "final"?
    I know that final classes cannot be extended. Other than this,
    Is there any thing that will improve the compiler time for the final classes?

    compiler performance is utterly unimportant anyway...
    If the compiler takes too long, just get a faster build system :)

  • What is better import or full declare a class ?

    I have a class to draw
    If I write
    Myclass(Graphics2D g) { .....
    it is neccesary to import java.awt.Graphics2D;
    but if I write
    Myclass(java.awt.Graphics2D g) { .....
    it is not.
    Can anybody explain me the pros and cons ?

    jverd wrote:
    tonnot wrote:
    Can anybody explain me the pros and cons ?Pros of importing: You code is easier to write and easier to read.
    Cons of importing: None.Although its worth mentioning the very rare corner case whereby two classes your class needs to use, have the same simple name. Eg, you need to use both List from the collections API and also List from AWT. In cases like that, you'll need to fully declare one of them and import the other, or more likely import neither, to remove any ambiguity for anybody reading the code. But that hardly ever happens. I've only seen it a couple of times, and one of them was down to inconveniently-named generated code, which I eventually just gave in and changed the generation of.

  • Declaring public class in JSP environment.

    Can anybody please tell me, what technical implication can occur, if we declare a public java class inside JSP.
    The scenario is -
    I have a JSP file used to display the results in table format.
    Before displaying the table, a public class is defined in the same JSP.
    Inside the scriptlet, the object is instantiated (using 'new' keyword).
    That object is used to populate the table.
    I know that according to the MVC architecture, the presentation logic should not be intermingled with the business logic. But I wanted to know whether any 'technical' problems we can face because of this.
    For e.g. Synchronization problem or singleton object creation problem etc.

    Yes its like a inner class, and will be available only to the current JSP.
    The code structure is somewhat like this -
    <%!
    public class SearchCN{
         String strUserName;
         String strPassword;
         Context context = null;
    %>
    <%
    SearchCN searchObject = new SearchCN();
    obj.connect();
    %>
    Now in such a scenario, I can think of some causes like, if N no of users open the same JSP, then N connections will get created. Connection sharing which could have been implemented for better performance is not possible.
    Can you give any additional inputs?

  • Forward Declaration in Class Builder

    Hi All,
       In the Class Builder (SE24)when i create a new Class, There is an option of Forward declaration  in the Property Tab. In this, we can add Type Group Interface and Class. What is the Purpose of this forward declaration?
      The F1 help does not provide any help.
    Regards,
    Kapil.

    in addition to above replies, it can be used for classes and interfaces in that case Forward declaration is equivalent to
    CLASS <class_name> DEFINITION LOAD.
    interface <interface_name> load
    statements which are normally used in Program ( case for local classes )
    use of Load from SAP docu
    ... LOAD
    Effect
    The variant with the LOAD addition loads a global class class from the Class Library. This statement was needed before Release 6.20 if you wanted to access one of the static components of class from within a program, or to declare an event handler for class before class had been loaded automatically. From Release 6.20 onwards, the LOAD addition is only needed if the compilation of an ABAP program fails because it includes recursive accesses of a globa l class. In such cases, you may be able to make the program compilable by explicitly loading the class before recursion.
    Thanks,
    kranthi.

  • Problem declaring new Class instance

    I would appreciate your help, I am new to programming & decided to learn Java.
    The two programs I have tried to write myself have not compiled for the same error:
    Javac tells me that it cannot resolve the " = new " syntax, it sees NEW as a variable.
    Here is the code for the latest:
    public class DBQuery1 {
         String username, password;
         boolean getOut = false;
    public class PassDB extends javax.swing.JFrame implements ActionListener {
         JTextField uname = new JTextField(10);
         JPasswordField pword = new JPasswordField(10);
         // codePhrase.setEchoChar('*');
         public PassDB() {
              super("duBe's database logon");
              setSize(260, 160);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              JPanel pane = new JPanel();
              JLabel unameLabel = new JLabel ("Username: ");
              JLabel pwordLabel = new JLabel ("Password: ");
              JButton submit = new JButton("OK");
              submit.addActionListener(this);
              pane.add(unameLabel);
              pane.add(uname);
              pane.add(pwordLabel);
              pane.add(pword);
              pane.add(submit);
              setContentPane(pane);
              setVisible(true);
              if (getOut == true)
                   PassDB.close();
         public void actionPerformed(ActionEvent evt) {
              PassDB clicked = (PassDB)evt.getSource();
              username = uname.getText();
              password = pword.getText();
              getOut = true;
         public static void main(String[] arguments) {
              PassDB UPass = new PassDB();
    it goes on, but the problem seems to be with the declaration of " UPass ".
    There are other problems with this code that I cannot fix at the moment, but I'll ask those questions when I get this problem fixed.
    Any help will be much appreciated, I know it is something silly I have done, but I can't find the problem myself.
    duBe

    The two programs I have tried to write myself have not
    compiled for the same error:
    Javac tells me that it cannot resolve the " = new "
    syntax, it sees NEW as a variable.It would be interesting to see the error message you are getting - what you have described sounds rather odd - I would expect an error along the lines of "unqualified enclosing instance" or "this can not be referenced from a static context".
    You have declared PassDB as an inner class (it is nested inside another class and is not declared static). As somebody else has already said, if you are new to Java, you might be better not doing that for now.
    To instantiate an inner class, you need an instance of the deepest nested lexically enclosing class. I'm not going to go into detail about that in the New To Java Technology forum, but if you don't already know or understand that, then perhaps inner classes are too much to be tackling right now.
    The syntax for instantiating PassDB is x.new PassDB(), where x is an instance of DBQuery1. Thus new DBQuery1().new PassDB() should compile (though it might not be want you really want). Another option open to you is to use static nested classes - just declare the PassDB class static, and treat it as you would any other class (though now you won't be able to access the non-static fields of DBQuery1 directly)

  • Help with Declaring a Class with a Method and Instantiating an Object

    hello all i am having trouble understanding and completing a lab tutorial again!
    im supposed to compile an run this code then save work to understand how to declare aclass with a method an instantiate an object of the class with the following code
    // Program 1: GradeBook.java
    // Class declaration with one method.
    public class GradeBook
    // display a welcome message to the GradeBook user
    public void displayMessage()
    System.out.println( "Welcome to the Grade Book!" );
    } // end method displayMessage
    } // end class GradeBook
    // Program 2: GradeBookTest4.java
    // Create a GradeBook object and call its displayMessage method.
    public class GradeBookTest
    // main method begins program execution
    public static void main( String args[] )
    // create a GradeBook object and assign it to myGradeBook
    GradeBook myGradeBook = new GradeBook();
    // call myGradeBook's displayMessage method
    myGradeBook.displayMessage();
    } // end main
    } // end class GradeBookTest4
    i saved above code as shown to working directory filename GradeBookTest4.java
    C:\Program Files\Java\jdk1.6.0_11\bin but recieved error message
    C:\Program Files\Java\jdk1.6.0_11\bin>javac GradeBook4a.java GradeBookTest4.java
    GradeBookTest4.java:2: class, interface, or enum expected
    ^
    GradeBookTest4.java:27: reached end of file while parsing
    ^
    2 errors
    can someone tell me where the errors are because for class or interface expected message i found a solution which says 'class? or 'interface' expected is because there is a missing { somewhere much earlier in the code. i dont know what "java:51: reached end of file while parsing " means or what to do to fix ,any ideas a re appreciated                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Doesn't solve your problem, but this works for me...
    public class GradeBook
      public void displayMessage() {
        System.out.println( "Welcome to the Grade Book!" );
      public static void main( String args[] ) {
        try {
          GradeBook gradeBook = new GradeBook();
          gradeBook.displayMessage();
        } catch (Exception e) {
          e.printStackTrace();
    }

  • Method declaration in Class

    Hi Experts,
             In Classes and Methods i got one requirement like this , Please respond it.
    I have to create the method , where class is already defined(ZCL_CONFIGURABLE_ITEM )
    Create Method:  IS_SEQUENCE_NOT_TO_PRINT .
    Class name :ZCL_CONFIGURABLE_ITEM
    They had given me example logic also. But iam not able to do this because i have no idea on classes and methods.
    Please see this example and respond me .
    Create Method:  IS_SEQUENCE_NOT_TO_PRINT .  This must be a value method in class ZCL_CONFIGURABLE_ITEM , passing document number VBDKR-VBELN that returns a parameter value: X = True, Blank = false.
    GET BILLTO_PARTNER customer number for the document (reading VBPA where VBELN = document number VBDKR-VBELN and partner PARVW = ‘RE’ / ‘BP’)Read table KNA1 to get field KNA1-KATR6 where KUNNR equals Billto partner, and fields VBDKR-VKORG, VBDKR-VTWEG, VBDKR-SPART, Store first character of field KNA1-KATR6 in a work field e.g. W_ KATR6.
    Add the entry below to the ZLITERAL table in all clients
    Read the records from ZLITERAL table (reading key1 = C_ZLIT_GENOSYS_KEY, and key2 = C_KATR6_1) where C_KATR6_1 = attribute on class, with value = KATR6_1 .  You must add this attribute on the class.  Populate the internal table L_ZLITERAL_TAB type ZLITERAL_TAB_TYPE.

    Hi,
        You can create the method from the CLASS BUILDER. SE24. There navigate to methods tab and enter you method name. Then select visibility public or private.
    Then click on the parameters button to add parameters to the method. Selec IMPORT, EXPORT or RETURNING parameter for the function.
    Regards,
    Sesh

  • How to declare a class to be singleton ?

    A singleton pattern says that it will generate only one object all in a whole.
    Such a class is called to be singleton!
    How can we make a class in java to be singleton, that it generates only one object and further object generation is violated ?

    Like:
    public class MySingleton {
      private MySingleton() {  // private constructor prevents new from outside
    ... contents
      public static MySingleton getInstance() {  // get unique instance
         return _inst;
      private static MySingleton _inst = new MySingleton();
      }

  • What's "!" in method declaration in class?

    See the excerption below, what's the meaning of "!"?
    protected section.
      methods CALC
        importing
          !IV_PROC_TYPE type STRING optional
          !IV_PROC_NAME type STRING optional
          !IV_PROC_CLASS type STRING optional .
    Thanks in advance

    hi,
    the exclamation mark is nothing more than a hint to the ABAP compiler. It is required when you want use keywords like export, exceptions and so on as formal / actual argument.
    In the given scenario it is no must. But maybe the code is produced by some generator. For a generator not smart enough to no all rules, it is good advice to use exclamation marks for any parameter.
    Regards,
    anver
    <i>
    if hlped pls mark points</i>

  • Purpose of declaring the method or class or static and as instance

    what is the purpose of declaring a method in a class or the class itself as static.
    Does it mean we cannot create a copy of that class or method if we declare it as static.
    if so then why do they dont want that class to be created as a copy ?
    Why do they want to declare a class as static
    please provide some conceptual undersatnding regarding the static and instance class with one example

    Static methods are often used for the implementation of utility methods. Please have a look at the class CL_ABAP_CHAR_UTILITIES for example.
    You use the methods of this class in the same way as you would use a function in ABAP (like
    LINES( itab )
    ). You use it in a static way because the functionality is always the same no matter in what context you are calling the function.
    The purpose of instance methods is that their logic is in some way related to an attribute of the object instance that you use to call it.
    For example, you create an instance of object PO (a purchase order) called MY_PO. Then the method
    MY_PO->ADD_POSITION
    would add a position to a concrete PO that has a unique number etc. But if the object has a static method DELETE_POSITION then it just deletes the current position of a PO, regardless on which concrete PO you are acting at the moment.
    I hope this clarifies it for you.
    Regards,
    Mark

  • How to declare class for library like function

    Hey guys I want to declare class Math and define method subtrction so that i can use it in other class like
    package p1
    public class Math
    Math( ){}
    int Subtration ( int a, int b)
    return (a-b)
    package p2
    import p1.*;
    Public Class Test
    Test()
    int somenumber=Math.Subtration(3,4);
    For some reason I cannot see any methods inside Math class. when i type
    somenumber = Math. it show me only one option and that is 'class' instead of mathods inside Math. any thoughts?
    thx

    Yes I am aware of Math class in java. The question is
    how do i declare a class and call it's method in some
    other class with out declaring an object of that
    class.
    Like I can just call Math.sqrt(4) with out
    specifically declaring Math as an object. I want to
    declare my own class which has some methods I wish to
    call in some other class.MyClass.someMethod(). Just make sure that the methos is actually declared static. I suggest looking up the static keyword to find out what it means.

  • Class should be declared error - but it is declared!

    After compilation of my coding i get the following error* but i have declared the class FLowps. Please tell me how it shoud be declared....if code needs to be seen please e-mail at [email protected] and will send the code listing.
    Thankyou.
    *test.java:18: class Flowps is public, should be declared in a file named Flowps.java
    public class Flowps extends JApplet implements ActionListener
    ^
    1 error
    Tool completed with exit code 1

    <rant>
    Why won't people just read? The compiler prints out a perfectly comprehensible error message not only telling them what's wrong, but even how to fix it, and they just throw their hands up! "It's all too hard! Why can't someone just tell me the answer? Where's my tutor?"
    Arrgh! Try working with these people: code, code, code, ooops! Red flag! Can't code any more! I don't understand!
    The supposed difference between a computer and a human is that humans are supposed to get creative when faced with a difficulty, but some of 'em make you wonder.
    </rant>

  • How to declare interface within a class

    1. I want to declare interface within a class. How can i do that & whats the advantage ?
    2. How to declare class within a interface ?
    Can anybody tell me the concept ?/
    Thanks in advance
    madhu

    Classes and interfaces are types.
    What is the advantage of declaring a type within another one?
    Do you know what is the advantage of declaring a class within another class?

  • How to put multiple classes in a single file?

    Hello,
    I'd like to put mutliple classes in a single file. (This
    would be useful for grouping children that are minor extensions of
    parent classes or helper classes that are used by one class only).
    When I tried to put two classes in one file, I got this error
    message:
    5006: An ActionScript file can not have more than one
    externally visible definition: Notation.editField,
    Notation.labelField1
    This is the structure I used. Thanks in advance for your
    help.

    You can declare multiple classes in a single file, but only
    one can be
    within the package declaration. All class declarations
    outside the package
    are invisible to code outside the file.
    package sample
    public class SampleClass
    class SampleClassHelper
    class SampleClassHelper2

Maybe you are looking for

  • How do I transfer my bought itunes songs to my MAC from my Windows PC?

    Is it also possible to transfer my songs from my IPOD shuffle to my MAC?

  • Problem while posting to accounts..

    Hi experts, im getting the following errors after  posting to accounts.The incorrect documents have been created.there were 2 error messages. +1.No creditor with the search key 1100 LFB1 PERNR 00002338 has been found+ Message no. 3G213 Diagnosis No v

  • Reminders will not show up under location services

    I'm trying to set a reminder when I'm at a location. BUT - I go to Privacy -> Location Services -> and the Reminders Ap doesn't even appear in the list. (see picture) I have tried to reset the Location Services by turning off and back on. Also by tur

  • Private Interconnect: Should any nodes other than RAC nodes have one?

    The contractors that set up our four-node production 10g RAC (and a standalone development server) also assigned private interconnect addresses to 2 Apache/ApEx servers and a standalone development database server. There are service names in the tnsn

  • Changing default button programmatically

    I have a set of 3 buttons on a form, and one is designated as the default button. When it is selected, I would like a different button to become the default, but I can't find a reference to a suitable property in the set_item_property built-in. The q