Class inside main method?

can we write class inside tne main method in java? If yes kindly explain with example.

> can we write class inside tne main method in java? If
yes kindly explain with example.
The Java? Tutorial - Nested Classes
~

Similar Messages

  • To define a class inside a method

    Hi,
    Iam new to OOPS ABAP.
    I had one doubt,
    Can we define and implement a new class inside a method... end method (like below)
    Method M1
    Class C1 Defination.
    Class C1 Implementation
    Method
    Endmethod
    Endmethod
    Please suggest me on this how to proceed.
    Regards
    Srinivas

    Hi Rich Heilman/Naimesh
    The requirement is
    In XI Proxy(XI proxy is related to creation of materials in SAP),there are macros defined, and my client told us to find out wheather is it possible to replace the macros with the classes and methods.
    Can i define a global class and call that method inside the class.
    Method M1
    Define a global class oref
    create object.
    Call method oref->m2.
    Endmethod
    Can i do it in the above way.Please suggest.
    Regards
    Srinivas

  • JFrame inside main method

    I have a JFrame object called mainJFrame constructed inside the main method in a program which I have made. Later I would need to use the getWidth() on the mainJFrame object in another class. Don't know how to design this. I tried to make the mainJFrame a public constant inside the class which contain the main method.
    This is a school project and in the specifications it is written that all the object have to be private, so I guess I cant make it as an public constant which I have made it. my program is already working but I just need to have it running with the mainJFrame object reference not as a public reference.
    /Thanks in regard from a future Java programmer

    bold This is where I call the JFrame....(it is called just frame and not mainFrame which I wrote earlier) bold
    public class BouncePanel2 extends JPanel implements ActionListener {
         public void actionPerformed(ActionEvent e) {
              if(e.getSource() == timer) {
    setSize( ( bold BounceMain.frame.getWidth() bold /2)-4, (BounceMain.frame.getHeight() )-32);
    bold this is a working version of the class containing the main method bold
    public class BounceMain {
         bold public static final JFrame frame= new JFrame("Bounce slutversion"); bold
         public static void main(String[] args) {
              MainPanel panel = new MainPanel();
              frame.getContentPane().add(panel );
              frame.setSize(604,632);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.pack();
              frame.setVisible(true);     
    bold an this is another working verison bold
    public class BounceMain {
         static public JFrame frame;
         public static void main(String[] args) {
              bold frame = new JFrame("Bounce slutversion"); bold
              MainPanel panel = new MainPanel();
              frame.getContentPane().add(panel );
              frame.setSize(604,632);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.pack();
              frame.setVisible(true);     
    but both these versions contains a public JFrame reference
    I will look at your reply which you wrote but I just wanted to write this if case it would clarify anything...

  • Inner class inside a method - how does it access method's local variable?

    hello All:
    I've learnt that, an inner class, if defined inside a method, it can access the method's local variables, only when they are defined as "final".
    Anyone can help explain the rationale behind it?
    Thanks a lot!
    Sway

    fathomBoat wrote:
    In java, everything is about pass-by-reference.
    Wrong! Nothing in Java is ever pass-by-reference.
    Java uses pass-by-value everywhere.
    It makes sense to me if the reason of enforcing a variable to be "final" is to prevent it being messed up.No, the reason is that a copy is made and if the variable weren't final then it could change later on and the developer could be confused because his inner class didn't "see" that change.
    The variable being final prevents that scenario.
    However, if a copy of the variable is made inside the inner class, i dont see how possible it could affect variables outside of class?Such a copy is made, but the language designers wanted to hide that fact from the developer. By forcing all accessed variables to be declared final the developer has no way to realize that he's actually working on a copy.

  • Access instance method of a class inside instance method of another class

    Hi Friends
    I have to use one c1->m1.
    c1 is a public instantiation and m1 is a public Instance Method.
    I called this m1 method by creating c1 object for class c1.
    But problem is inise M1 method,i have a statement
    CALL METHOD mo_central_person->if_hrbas_pd_object~read_infty.
    here mo_central_person is of type some other class c2.
    As without creating the object,i didn't use the above interface method.
    Can please suggst me how to handle this.
    Regards,
    Sree

    class a definition.
      public section.
      methods : display,
                disp.
      data : a(10) type c value '10',
             b(5) type c value 'abc'.
    endclass.
    class a implementation.
      method display.
        write : a.
      endmethod.
      method disp.
        write : b.
      endmethod.
    endclass.
    class b definition inheriting from a.
      public section.
      methods : display1.
      data : c(7) type c value '7'.
    endclass.
    class b implementation.
      method display1.
       call method display( ).
        write c.
      endmethod.
    endclass.
    start-of-selection.
    data : o_obj type ref to b.
    create object o_obj.
    call method o_obj->display1.
    the method display is in class a.
    we can call this method in class b using the following statement.
    method display1.
       call method display( ).
        write c.
      endmethod.

  • How to increase the Java Heap Space for a main method

    Hello all,
    after a given number of calculations inside a class with main-method, I get an OutOfMemoryError (Java Heap Size).
    So I'm just trying to increase the Heap Size Creator uses, but it won't work.
    After reading a lot of posts and online-articles, I tried the following:
    1) I changed the -Xms and -Xmx options (to 512m) both in
    $CREATOR/SunAppServer8/domains/creator/config/domain.xml and
    $CREATOR/etc/creator.conf (this file also points to the Creator built-in JDK)
    2) additionally I tried to start Creator from the console with the respective option:
    ./creator -J-Xmx512m
    The memory toolbar shows the right value, but when I use
    System.out.println("JVM maximum memory:\t"+Runtime.getRuntime().maxMemory()/1024+" KB\n");it always says 64 MB, no matter which option I chose in the config-files.
    So how can I increase this value?
    Regards,
    Felix
    Message was edited by:
    Felice
    Message was edited by:
    Felice

    Thank you very much,
    this was an important hint. Additionally I had to create a new "Java Class Library" project with a copy of the needed files (those that are not dependent on the Application Server). Then I could use the option you mentioned.
    Unfortunately, when I try to run the file with
    java -jar FILENAME.jarit won't work, because the archive is not complete: MANIFEST.MF lacks the main-class and all involved libraries.
    I managed to add the main-class attribute manually, but adding all library references seems to be very error-prone.
    So does anyone know if I did something wrong or if this is a bug of Creator?
    Regards,
    Felix

  • Invoking main method from cmd line (deployed as web app)

    I have a application deployed as a web application (war file).
    I want to create a regular java class with main method so that main can invoked
    on a command line.
    The main method wishes to share resources (DB connection pool etc) with the rest
    of the web application.
    Is it possible ? and how to invoke the main method inside the war(not jar) ?

    Hi,
    if you deployed this to OC4J, open EM
    http://server:port/em
    and get to the Web service. You can execute it from here to see if generally this is working, or if you have a proxy problem only
    Frank

  • Object life in main method after frame created

    hi
    i'm having a slight problem with the life of an object i'm creating in the main method of my application.
    i have a JDesktopPane that is initialised in the main class's main method and constructor. within the main method i also add the first internal frame to the desktop.
    after the user completes the details of the first frame, another is called (from this first frame) and the first frame is disposed.
    the problem is that although all subsequent internal frames added to the desktop outside the main class, they are garbage collected with no problems (i'm using a profiler), however the first frame and the frame created from that first frame are never collected due to the reference maintained in the main class. i've tried adding the creation of these frames outside the main class but given that these methods are called from main the reference is still maintained.
    is there any way to create those first 2 frames without the reference being maintained in the main class such that they become elegible for garbage collection? do i have it all wrong perhaps and my approach is bad?? any help would be greatly appreciated.
    thanks heaps.
    Takis

    hi
    thanks for your reply.
    i tried that and it didn't work. the instance of that first internal frame is still there.
    i really am at a loss with this one.
    any other suggestions would be greatly appreciated.
    Takis

  • How can I run my doclet from main method?

    I want to run doclet from main method.
    My source is:
    command = String.format("%s%s%s%s",
    "cmd /c ",
    "% javadoc -doclet ",
    "ExportJavaLib ",
    "-private " + dir.getAbsolutePath() + ".java");
    Runtime.getRuntime().exec(command);My doclet class file is in the same directory where is my class containing main method.
    I run the main class from bat file and set
    set CLASSPATH=%CLASSPATH%;D:\jdk1.5.0_06\lib\Tools.jarBut my code does nothing when it must execute the doclet.

    I've done it. 10x :)

  • 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();
    }

  • Renaming main method class.

    When compiling my java classes, I find that they are always compiled
    to a file name of X.class.
    If I only ever want to rename one class file, the class file with my main method
    inside it, and have
    java NewClassName.newExtention
    run it as per normal, may I do that, and how do I have it run?
    What may I specify in an executable jar file's manifest main-class entry to have this work as well?

    I use a free jar wrapper tool, that for reasons I won't go into I'm locked into.
    It mentions the main-class manifest entry, and so the main class .class name,
    in plain text in the result wrapped file unfortunately.
    Which after some research isn't too much of a problem, since
    I can't access method or constructor class innards with reflection;
    since the default class loader will only get me as far as a class reference.
    Correct?
    So my only question is...
    Java Class Loading.

  • Public main method in a non-public class

    Hi,
    It is said that main method in a java class must be public so that it can be invoked by JVM. This works even if the class itself has default modifier.
    But while programming, any method (be it public) of a class with default access modifier from one package can't be accessed by classes from other packages. For example,
    1. Consider class A in package a.
    package a;
    class A {
    public void methA() {}
    2. Now consider class B in pakage b.
    package b;
    class B {
    void methB() {
    // Here it can't access class A and its method methA()
    So how does JVM invoke the main method of a class with default access modifier. Assume that the class is in some package a.b.c
    Regards,
    Amit

    Hi,
    Maybe this can also be beneficial for you.
    Standalone applications don't necessarily have init and start methods. They must have a public static void main routine to start things off. The main routine typically creates some object of the master class. It can't use any non-static variables or methods in the class with the implied this, since there is no this object. It typically quickly returns. Then the event loop processing starts. Even the static main method must live inside some public class, or at least in the top class, the first class and the class for whom the source file is named. There are no such thing as standalone static methods in Java. The signature of the main class must look exactly like this. Cut and paste it to be sure:

  • How can i call a main method  from a different class???

    Plzz help...
    i have 2 classes.., T1 and T2.. Tt2 is a class with a main method....i want to call the main method in T2......fromT1..is it possibl..plz help

    T2.main(args);

  • How to add a code TestIfElse(10) to the Main method of the Program.cs class?

    How to add a code  TestIfElse(10) to the Main method of the Program.cs class?
    Here is my code and I have copied from the Wiley book for Microsoft certification page 14,
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ifelse_Statement
        class Program
            static void Main(string[] args);
            TestIfElse(10);
            public static void TestIfElse(int n);
            if(n < 10)
                     Console.WriteLine("n is less than 10");
             else if(n < 20)
                    Console.WriteLine("n is less than 20");
             else if(n < 30)
                Console. WriteLine("n is greater than or equal to 30");
    Here is the error list I am getting,
    Error 1      Method must have a return type        C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\switch_Statement\Program.cs
    12 13
    switch_Statement
    Error 2
    Type expected C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\switch_Statement\Program.cs
    12 24
    switch_Statement
    Error 3
    Method must have a return type C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    12 9
    ifelse_Statement
    Error 4
    Type expected C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    12 20
    ifelse_Statement
    Error 5
    Invalid token 'if' in class, struct, or interface member declaration
    C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    16 9
    ifelse_Statement
    Error 6
    Invalid token '10' in class, struct, or interface member declaration
    C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    16 16
    ifelse_Statement
    Error 7
    Type expected C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    16 16
    ifelse_Statement
    Error 8
    Invalid token '(' in class, struct, or interface member declaration
    C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    18 35
    ifelse_Statement
    Error 9
    A namespace cannot directly contain members such as fields or methods
    C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    20 10
    ifelse_Statement
    Error 10
    Type or namespace definition, or end-of-file expected
    C:\Users\PVS\documents\visual studio 2013\Projects\Lesson01\ifelse_Statement\Program.cs
    30 1
    ifelse_Statement
    Error 11
    The type or namespace name 'n' could not be found (are you missing a using directive or an assembly reference?)
    c:\users\pvs\documents\visual studio 2013\projects\lesson01\ifelse_statement\program.cs
    16 12
    ifelse_Statement
    Error 12
    'System.Console.WriteLine(string, params object[])' is a 'method' but is used like a 'type'
    c:\users\pvs\documents\visual studio 2013\projects\lesson01\ifelse_statement\program.cs
    18 26
    ifelse_Statement

    static void Main(string[] args){
    TestIfElse(10);
    public static void TestIfElse(int n)
    if (n < 10)
    Console.WriteLine("n is less than 10");
    else if (n < 20)
    Console.WriteLine("n is less than 20");
    else if (n < 30)
    Console.WriteLine("n is greater than or equal to 30");
    Fouad Roumieh

  • Class extending a Frame with main method in it - how do I use the methods?

    Howdy.
    Having a problem.. I want my "main" class, ie. the class with the main method in it to extend a Frame class.. because the main method is static, I can't use Frame's methods.
    What's a trick to get around this ? I am making a deliberate design decision to extend the Frame class, because I thought it was.. well.. better. I could always just create a Frame object and utilise it.. but from little things I've read and seen it's better to extend it.
    When I use Forte to make a Frame program it sets up a constructor/etc. for the main class and then it does something akin to this (the class is called MazeGenerator) :
    public static void main(String args[]) {
    new MazeGenerator().show();
    What does "new MazeGenerator().show();" do when it's not being associated to an object handle ??
    Ta.
    - Scutt.

    Create an instance of the frame in main then the constructor can control it. Or you could add an init method or something:
    class MyFrame extends Frame {
    public static void main (String [] args) {
    MyFrame frame = new MyFrame();
    frame.init();
    You don't necessarily have to have a reference to an Object in order to create one. All the work can be done in the constructor (something I'm not really fond of however)

Maybe you are looking for

  • Error Involving EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap

    The JVM is crashing and I am getting the following error. I am having a hard time getting started figuring out why this is happening. I am not using this method directly and am having a hard time figuring out what is leading to this. In Eclipse when

  • Problem with Changing Data while Import through Excel Sheet

    Hi Experts, I am new to SAP LUMIRA and I am facing an issue with it. Actually, I am importing the data for analysis from an Excel sheet into SAP LUMIRA. I am able to prepare the visualization in the first go. But If I am changing the number of column

  • Caching data read from database

    I have a big performace problem in our J2EE application. We use jsp and servlets to process a request. Servlet gets the results from database which are stored in a vector of dataobjects and vector is stored in session. We display only 20 rows in jsp

  • About virtual channel in different device and/or different type channel

    Hi all,   I am looking for a way to write to several analog channels at the same time. In my code, I didnt' pay much attention to that, I just have them placed in the the same frame in the flat sequence such that they might be started to write simult

  • IMAC cant see WRT600N w/ WD MyBook HD

    I have just purcahsed the WRT600N and attached a western digital MyBook to it via USB All my XP machines have no problem mapping the hard drive nad using it but my iMAC when using the smb://<IP ADDRESS> to connect to it fails after entering the passw