Inner question

Hi,
Inner class question:
Q.1 Given:
public class MyClass {
public class MyRunnable implements Runnable
     public static void main(String [] args)
          (new Thread(new MyRunner())).start();
How to rectify it? change it to (new Thread(new MyClass().new MyRunner())).start(); ??
Thanks
gogo

What error do you get?? Let me guess: something about MyRunnable being used in a static context...
You cannot instantiate a non-static inner class in a static method (main in this case), because the instance of your inner class needs a reference to the instance of the enclosing class. Solutions: (1) make class MyRunnable static, or (2) instantiate it "within" an instance of class MyClass, as you suggested yourself.
Jesper

Similar Messages

  • 3 facts and 4 dimensions

    Hi,
    In my RPD I have 3 facts and 4 dimensions and the joins defined are as below
    Physical layer - All joins are FK and inner (1:N)
    FACTA-------------- Dim1,Dim2,Dim3,Dim4
    FACTB-------------- Dim1,Dim5,Dim6
    FACTC-------------- Dim1,Dim7,Dim8,Dim9
    BM layer - All joins are complex, inner
    Question is
    When i am fetching the data some records are showing blanck and some with records
    i wants table to be filled with records not blanck anywhere
    thanks

    If you set the content level for Fact B to be TOTAL for Dim 2 the metric will be null since there is no relationship between these tables. If you want the metric to repeat for this dimension you need to set the metric content level to TOTAL for Dim 2. Try that with one of the metrics and verify the behaviour.

  • Inner Class Question

    My question pertains to the code at the bottom of this post.
    I don't understand why the compiler doesn't give an error for the line below. Why would it let you refer to something inside the class (in this case I'm referring to ClassWithInnerClass.MyInnerClass) unless it were static?
    ClassWithInnerClass.MyInnerClass mic = cwic.retMyInnerClass();To illustrate why I'm asking, I created 2 ints ("regularInt" and "staticInt") inside class "ClassWithInnerClass". The compiler let me set the value of "staticInt" from within main whereas it wouldn't let me do so w/ "regularInt" (which is why I commented that line out). Don't get me wrong though - I understand the reasons why the compiler behaves as it does for "regularInt" and "staticInt". I understand that a static variable can be accessed without instantiating a class (and that there's only 1 created no matter how many classes are instantiated). I also understand that, to access a non-static variable, you need to instantiate a class. My question arises only because of trying to extend that logic to MyInnerClass.
    I can already take a guess that the answer is going to be something like, "the reason it works this way is because class 'MyInnerClass' is just a declaration NOT a definiton". I guess I just want confirmation of this and, if possible, some reasoning behind this logic.
    HERE'S THE CODE...
    class CreateInnerClasses {
         public static void main (String args[]) {
              ClassWithInnerClass cwic = new ClassWithInnerClass();
              ClassWithInnerClass.MyInnerClass mic = cwic.retMyInnerClass();
              //ClassWithInnerClass.regularInt = 5;
              ClassWithInnerClass.staticInt = 10;
              mic.printIt();
    class ClassWithInnerClass {
         public int regularInt ;
         static public int staticInt;
         class MyInnerClass {
              void printIt() {
                   System.out.println("Inside ClassWithInnerClass.myInnerClass");
         MyInnerClass retMyInnerClass () {
              return new MyInnerClass();

    The line    ClassWithInnerClass.MyInnerClass mic = cwic.retMyInnerClass();is accepted because the name of the inner class is ClassWithInnerClass.MyInnerClass. This has nothing to do with accessing fields even though the syntax is similar.
    On the other hand, the line    SomeClassWithAnInnerClass.InnerClass ic = new SomeClassWithAnInnerClass.InnerClass();is not accepted because the nested class SomeClassWithAnInnerClass.InnerClass is not static: you must have an instance of the outer class available. The correct syntax for calling the constructor of the inner class would be    Outer.Inner instance = outerInstance.new Inner();In this case you could write:    ClassWithInnerClass.MyInnerClass mic =  new SomeClassWithAnInnerClass() . new InnerClass();
        // OR:
        ClassWithInnerClass.MyInnerClass mic =  cwic . new InnerClass();The Java tutorial has a pretty good explanation on nested classes:
    http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
    http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html

  • Inner classes question

    Hi guys,
    I have a question to ask - when i'm writing an inner class and i want to create a new instance from it, the Eclipse forces me to use an instance of the containing class to call new.
    Is it how it's done or am i doing something wrong?
    public class A
        // Some code here
        public class B
             //Some code here
    }Trying to create an instance:
    A a = new A();
    A.B b = a.new B();

    What you have defined is "non-static" inner class.A "non-static" can only be instantiated only if we create an object of outer class.True.
    >
    There are two ways to create "inner" class object,
    1)The way you have written the codeTrue.
    2)
        public class A
    {    // Some code here    
    public void createInner()
    classB b=new ClassB();
    public classB   
    //Some code here   
    That code won't compile. Even if you corrected it so that it did compile, it doesn't really help in the OP's situation. Did you read the advice of Saish and Jos about why class 'B' probably shouldn't be public, anyway?

  • Question about Inner/Outer classes

    Hello!
    I'm still preparing for java cert exam and have a question. If I have Outer and Inner classes like this:
    Outer o = new Outer();
    Outer.Inner i = o.new Inner();Does this mean that 'i' has a reference to both Outer and Inner classes. Moreover, does 'i' have a reference to the 'this' of 'o'?
    Thanks,
    Victor.

    "i" doesn't have a reference. "i" is a reference. I think you are trying to express this:
    import java.lang.reflect.*;
    public class Outer {
        class Inner {
            private int foo;
        public static void main(String[] args) {
            Outer o = new Outer();
            Outer.Inner i = o.new Inner();
            for(Field field : i.getClass().getDeclaredFields()) {
                System.out.format("%s %s %s;%n", Modifier.toString(field.getModifiers()),
                    ((Class)field.getGenericType()).getName(), field.getName());
    }

  • Design question on inner class

    I have an app, with a JPanel. The way I have it setup right now is the JPanel is an inner class with an overloaded constructor. I call the inner class depending on the arguements. this is done repeatedly in my program.
    My question is: Should I be doing it this way or maybe as a method that returns the updated JPanel? Can this cause serious memory problems?
    example
    private void CreateTextfields(int q, int c){
    // c =      number of choices selected from other JComboBox
    // q =      question selected from combo box
    // questions is the arrylist containing all questions
       AnswerPanel answerPanel;
       answerPanel = null;
       textFieldPanel.removeAll();
    if( questions.isEmpty() && c > 0 ){
             answerPanel = new AnswerPanel(c);
    else if( !questions.isEmpty() && c == 0 ){
             answerPanel = new AnswerPanel(q, listOfAnswers);
    else if( !questions.isEmpty() && c > 0 ){
             clearLogic();
             answerPanel = new AnswerPanel(q, listOfAnswers, c);
             textFieldPanel.add( answerPanel, BorderLayout.CENTER ); Thanks
    Jim

    <<serious memory problems>>
    Probably not. But it seems wasteful to keep creating a new object just to set some properties. Why not make a single AnswerPanel, with overloaded setContents() functions?
    Not that I'm a Swing expert or anything....
    HTH,
    Ken

  • Question on inner classes

    I have a class that has 3 inner classes, on class read the colors and font settings, how can I pass this class to the 2 other inner classes
    Here is a watered down version of code:
    class MainClass{
           public MainClass(){
                 // do some stuff
                 innerClass3  ic = new innerClass3();// I need to pass this to the other inner classes
                 layeredPane.add( new innerClass1( some Variable), 1);
                 layeredPane.add( new innerClass2( anotherVariable), 2 );
    class innerClass1 extends JTextPane{
         public innerClass1( String s ){
    // instead of doing this
              innerClass3 ic = new innerClass3();
              setBackground(ic.getBackground() );
    class innerClass2 extends JPanel{
            public innerClass2( String[] z){
             // and this again
                innerClass3 ic = new innerClass3();
                setForeground(ic.getFontClor());
    class innerClass3{
    Color background;
    Color foreground;
        public innerClass(){
         setColors();
    public void setColors(){
       background = Color.blue;
       foreground = Color.red;
    public Color getBackground(){
       return background;
    public Color getFontColor(){
    return foreground;
          

    Thanks for the reply
    Works well for first screen, but I recall the innerClasses(1 and 2) from an actionPerformed in innerClass2 and I keep getting nullPointerError when referring to innerClass3. Each screen will have different fonts and colors, so I need it to update after each button click.
    // this is inside innerClass2( it's panel with buttons )
    // the next screens graphics will depend on which button is selected
    public void mouseReleased(final java.awt.event.MouseEvent e) {
             layeredPane.removeAll();
              currentQuestionNumber++; 
              question = questionText[currentQuestionNumber] ;  
    // adds a JTextPane with question         
              layeredPane.add(new innerClass1(question, ic), 1 );
    // adds a JPanel with buttons
              layeredPane.add(new innerClass2( currentQuestionNumber, ic ), 3 );Will be tearing hair out soon
    Jim

  • A question about non-static inner class...

    hello everybody. i have a question about the non-static inner class. following is a block of codes:
    i can declare and have a handle of a non-static inner class, like this : Inner0.HaveValue hv = inn.getHandle( 100 );
    but why cannot i create an object of that non-static inner class by calling its constructor? like this : Inner0.HaveValue hv = Inner0.HaveValue( 100 );
    is it true that "you can never CREATE an object of a non-static inner class( an object of Inner0.HaveValue ) without an object of the outer class( an object of Inner0 )"??
    does the object "hv" in this program belong to the object of its outer class( that is : "inn" )? if "inn" is destroyed by the gc, can "hv" continue to exist?
    thanks a lot. I am a foreigner and my english is not very pure. I hope that i have expressed my idea clearly.
    // -------------- the codes -------------------
    import java.util.*;
    public class Inner0 {
    // definition of an inner class HaveValue...
    private class HaveValue {
    private int itsVal;
    public int getValue() {
    return itsVal;
    public HaveValue( int i ) {
    itsVal = i;
    // create an object of the inner class by calling this function ...
    public HaveValue getHandle( int i ) {
    return new HaveValue( i );
    public static void main( String[] args ) {
    Inner0 inn = new Inner0();
    Inner0.HaveValue hv = inn.getHandle( 100 );
    System.out.println( "i can create an inner class object." );
    System.out.println( "i can also get its value : " + hv.getValue() );
    return;
    // -------------- end of the codes --------------

    when you want to create an object of a non-static inner class, you have to have a reference of the enclosing class.
    You can create an instance of the inner class as:
    outer.inner oi = new outer().new inner();

  • Design question about when to use inner classes for models

    This is a general design question about when to use inner classes or separate classes when dealing with table models and such. Typically I'd want to have everything related to a table within one classes, but looking at some tutorials that teach how to add a button to a table I'm finding that you have to implement quite a sophisticated tablemodel which, if nothing else, is somewhat unweildy to put as an inner class.
    The tutorial I'm following in particular is this one:
    http://www.devx.com/getHelpOn/10MinuteSolution/20425
    I was just wondering if somebody can give me their personal opinion as to when they would place that abstracttablemodel into a separate class and when they would just have it as an inner class. I guess re-usability is one consideration, but just wanted to get some good design suggestions.

    It's funny that you mention that because I was comparing how the example I linked to above creates a usable button in the table and how you implemented it in another thread where you used a ButtonColumn object. I was trying to compare both implementations, but being a newbie at this, they seemed entirely different from each other. The way I understand it with the example above is that it creates a TableRenderer which should be able to render any component object, then it sets the defaultRenderer to the default and JButton.Class' renderer to that custom renderer. I don't totally understand your design in the thread
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=680674
    quite yet, but it's implemented in quite a bit different way. Like I was saying the buttonClass that you created seem to be creating an object of which function I don't quite see. It looks more like a method, but I'm still trying to see how you did it, since it obviously worked.
    Man adding a button to a table is much more difficult than I imagined.
    Message was edited by:
    deadseasquirrels

  • Question about inner class - help please

    hi all
    i have a question about the inner class. i need to create some kind of object inside a process class. the reason for the creation of object is because i need to get some values from database and store them in an array:
    name, value, indexNum, flag
    i need to create an array of objects to hold those values and do some process in the process class. the object is only for the process class that contains it. i am not really certain how to create this inner class. i tried it with the following:
    public class process{
    class MyObject{}
    List l = new ArrayList();
    l.add(new MyObject(....));
    or should i create the object as static? what is the benifit of creating this way or static way? thanks for you help.

    for this case, i do need to create a new instance of
    this MyObject each time the process is running with a
    new message - xml. but i will be dealing with the case
    where i will need a static object to hold some
    property values for all the instances. so i suppose i
    will be using static inner class for that case.The two situations are not the same. You know the difference between instance variables and static variables, of course (although you make the usual sloppy error and call them static objects). But the meaning of "static" in the definition of an inner class is this: if you don't declare an inner class static, then an instance of that inner class must belong to an instance of its containing class. If you do declare the inner class static, then an instance of the inner class can exist on its own without any corresponding instance of the containing class. Obviously this has nothing to do with the meaning of "static" with respect to variables.

  • Some questions on the inner workings of Stellent IBPM 7.6

    Hello,
    I'm having some trouble figuring out a Stellent IBPM 7.6 installation at one of our customers' sites.
    I must admit that I am completely new to IBPM, but since our customer was unable to find anyone who still supported this software,
    they have asked me to find out whether it is possible to make a small change.
    They are using it to archive tiff files + metadata (in text files) exported by Kofax.
    I was unable to find any documentation on version 7.6, but I was able to find an administrator's guide of Oracle Imaging and Process Management 7.7, which I have been using so far.
    So here's the thing;
    I've largely been able to figure out how the whole Filer Server works, how it's configured, how applications are defined in the Application Definition editor,
    how meta data and image files get stored based on the defined indexes/fields and storage classes,
    how galleries are made, how users are linked to them and how searches can be built using the Search Builder.
    So far so good. I understand how the current setup is functioning.
    But what I haven't been able to find in the documentation that I posses,
    is how the system deals with any changes made to this setup.
    More specifically: changes made to the definition of indexes and fields.
    The main questions I have at this moment are:
    - How does the database, specified under "output" in the application definition editor, get constructed?
    Does this have to be done manually? And do you just have to name tables and columns exactly as you specify them in the Application Definition?
    Or will a new table automatically be created when I define a new application?
    I assume it will, because I noticed that the names of the tables in the database are <Application Name>+<Index Name>.
    But I haven't been able to find any piece of information on this, and I don't want to base any actions on assumptions.
    - Is it possible to add a field to an application that has already been online and filed?
    And also in this case; what happens to the output database? Do columns get added automatically or is this a manual step?
    I hope that despite the age of this software, someone will still be able to answer these question or point me to some documentation that I have missed.
    Kind regards,
    Wouter

    To answer the questions:
    #1   An interrupt service routine (ISR) running a lower IRQL may be interrupted by another ISR running at a higher level.
    #2   Having never heard of a thread interrupt, all I can say here is that a thread may be pre-empted for a lot of reasons.
    #3   An ISR does not have to schedule a DPC unless it needs to do work that it cannot do at high IRQL.
    #4   Assuming that the Write and IOCTL are on seperate queues, yes you need a a lock to protect shared resources.
    #5   As the name implies kernel dispatcher objects allow scheduling (i.e. dispatching of another thread), while things like spinlocks do not. 
    #6    Timers are just another dispatching object, i.e. one can wait on them the same way one waits on a mutex or a semaphore.  A WDF TIMER is basically a wrapper around a regular timer to take care of some of the housekeeping (particularily
    with respect to stopping the driver etc)
    #7     If a routine is running at DISPATCH_LEVEL you are limited to spin locks for synchronization. You can with a zero timeout check the status of a kernel dispatcher object.   In general, routines like this should be
    designed to only use spinlocks.
    #8    An arbitrary thread is just that, it may be a thread in any process. Basically a currently running thread is grabed and used to run the interrupt so that the scheduler which has overhead and is not designed to run at interrupt level
    IRQL's does not need to run.
    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

  • Inner Margin Question

    Hi everyone,
    I am designing an art catalog on 240 pages, size 200x280 mm, which will be perfectly bound.
    I wanted to have the outer margin larger than the other margins, however, I am not sure about the inner margin size - I originally set it at 15 mm, but now I am thinking it should be larger?
    Also, should I use inner margin bleed, as I have 2-page images on some of the pages?
    Thanks for your help.

    Is there a special reason for making your book just slightly smaller than A4 - your size doesn't seem to be one thing or the other.
    Margins are rather subjective. I suggest you go into a bookshop and look at books that are around your selected size and with the kind of design and content you want  and see which margins seems to work best.
    You wouldn't have inside bleed on books that are printed in sections.
    If your book is to be perfect bound, you will also need to produce cover artwork. This will be printed separately on heavier stock than the text and maybe laminated. The flat size of the cover artwork should be twice the size of the text trimmed page size plus and allowance for the spine.
    Design your book in double page spreads and supply the printer with single page PDFs with printers marks. Discuss with your printer all technical matters before proceeding.
    Derek

  • Inner thread question

    class A {
    getName(){}
    setName(){}
    class B extends A{
    private class innerClass extends Thread{
    run(){
    getName()
    setName()
    The problem is that calling getName from inner class will use the getName from Thread beacuse that is it's super. How do I specify the getName from class A? The setName is OK because Thread does not have a setName

    The only way you could do this remove the extends A and call explicitly by creating an instance of A
    run(){
    A a = new A();
    a.getName()
    a.setName()
    Doug_99 wrote:
    class A {
    getName(){}
    setName(){}
    class B extends A{
    private class innerClass extends Thread{
    run(){
    getName()
    setName()
    The problem is that calling getName from inner class will use the getName from Thread beacuse that is it's super. How do I specify the getName from class A? The setName is OK because Thread does not have a setName

  • About a question using Update ... inner join ?

    select *
    FROM a
    INNER JOIN b ON a.ProductID=b.ProductID
    WHERE a.HeadID='000246'
    this statement is ok ;
    But the following statement does not work ! Why ?
    UPDATE a SET
    a.Quantity=a.PurchaseQuantity/b.ConversionGene
    FROM a
    INNER JOIN b ON a.ProductID=b.ProductID
    WHERE a.HeadID='000246'

    "Because Oracle syntactically does not support that type of construct..." Is a correct statement, but not because "It expects only one table in UPDATE statement". The synatax for an updateable join in Oracle requires a "proper" in-line view to be updated.
    As long as the table joined (in my example t1) has a declared unique constraint on the columns used to join by (in my example id), you can do it like:
    SQL> SELECT * FROM t;
            ID DESCR
             1 One
             2 Two
             3 Three
    SQL> SELECT * FROM t1;
            ID DESCR2
             1 Un
             2 Deux
    SQL> UPDATE (SELECT t.descr, t1.descr2
      2          FROM t
      3             JOIN t1 ON t.id = t1.id)
      4  SET descr = descr2;
    2 rows updated.
    SQL> SELECT * FROM t;
            ID DESCR
             1 Un
             2 Deux
             3 ThreeTTFN
    John

  • Inner classes - a general question

    i'm having some difficulties understanding when to use a static inner class
    and when to use a non static inner class
    lets say i'm implementing a LinkedList , and i want my nodes and my iterator
    implemented in inner classes .
    when and why should i declare them static or non static.
    thank you very much in advance.

    Inner classes are never static. Nested classes can be either static or non-static; a non-static nested class is called an inner class.
    Anyway, an inner class implicitly gets a reference to an object of the outer class, while a static nested class doesn't, so the decision should depend on whether or not it needs that.
    An iterator would need a reference to its collection so it would be a non-static nested class. A node wouldn't so it would be a static nested class.

Maybe you are looking for

  • JTable sorting - problem when adding elements (complete code inside)

    I�m writing this email with reference to a recent posting here but this time with the code example. (I apologize for the duplicated posting � this time it will be with the code) Problem: when adding more elements to the JTable (sorted) the exception:

  • Why do I have to update Keynote,Numbers,and Pages on a daily basis?

    For the past week I've had to update Keynote, Numbers, and Pages on a daily basis?If I update the apps today, tomorrow I will be asked to update them again.Something is wrong with picture. Is anyone else having this issue?

  • Buying an Appple Thunderbolt Display - any help on calibration?

    OK, I know already that the Thunderbolt Display is not the best monitor out there for color correction - at least that's what I've heard... but I have a new MBP (non-Retina) and I need both a larger display and the ports that are on the Thunderbolt.

  • Oracle Rules Manager - 11g Enterprise Ed. 11.2.0.3.0 64bit Prod - ORA-38441

    Hello Aravind: I'm having a problem when I try to create a rule class for an event with complex type attributes, but this is only happening in a database with this specs: - 11g Enterprise Edition 11.2.0.3.0 64bit Prod The previously specification is

  • Word 2008

    I am trying to delete my last page number of my document but after doing it all the numbers of the pages disappear. I started numbering from the third page but now iam having this problem. Any idea guys?