Results 1 to 5 of 5
  1. #1
    Arukas is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Trouble with Hello World Program

    I am trying a variant of the hello world program. The example I am working has you make your own JLabel class, I'm calling my GoodbyeDisplay. Now, as far as I can tell, I have the correct syntax according to the book. I did change a couple of names around.
    I've looked on the web and found how to make the code work. If I put Static in front of "class GoodbyeDisplay" it runs. However, the example does not use static.

    So my question is, what is going on here? How can I get my code to work without using the Static declaration?



    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package hellojava;
    import javax.swing.*;
    import java.awt.*;
    
    /**
     *
     * @author Revenant
     */
    public class HelloJava {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JFrame frame = new JFrame("Space for Rent");
            GoodbyeDisplay bye = new GoodbyeDisplay();
            frame.add( bye );
            frame.setSize ( 300, 300);
            frame.setVisible(true);        
        }
        
        class GoodbyeDisplay extends JComponent 
        {
            public void paintComponent( Graphics g )
            {
                g.drawString( "Goodbye Cruel World!", 125, 95 );
            }                
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Trouble with Hello World Program

    Do you mean the static that's part of the main method declaration? If so, sorry, that has to be there. Java requires at least one main method that is static and has the signature shown (String[] parameter, returns void, public), so it knows where to start the program.

    Oh, I see, you mean in front of the class GoodbyeDislay... your problem is that this is an inner class. If you don't make it static, then you have to create it on an instance of the outer class, something like:

    MyInnerClass foo = new MyOuterClass().new myInnerClass();

    Ugly. Solution: only use inner classes if you have a very good reason for doing so.

    Another solution is to move the InnerClass out of the OuterClass but in the same file, like so:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class HelloJava {
    
       public static void main(String[] args) {
          JFrame frame = new JFrame("Space for Rent");
          // GoodbyeDisplay bye = new HelloJava().new GoodbyeDisplay();
          GoodbyeDisplay bye = new GoodbyeDisplay();
          frame.add(bye);
          frame.setSize(300, 300);
          frame.setVisible(true);
       }
    
    }
    
    class GoodbyeDisplay extends JComponent {
       public void paintComponent(Graphics g) {
          g.drawString("Goodbye Cruel World!", 125, 95);
       }
    }
    But again, I don't recommend this unless you have a very good reason for doing this.

    Another solution is to create one file, GoodbyeDisplay.java, and simply have the main method inside of this. This is not a bad solution for simple programs.
    Last edited by Fubarable; 02-05-2012 at 12:23 AM.

  3. #3
    Arukas is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Trouble with Hello World Program

    Thanks, I got it to work. I also see what was the cause of the problem. I do have another question however. I don't quite understand your last suggestion. Are you saying put the GoodbyeDisplay class in another file?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Trouble with Hello World Program

    Yes. The best in this situation is to keep things simple and also to learn to use multiple source files.

  5. #5
    Arukas is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Trouble with Hello World Program

    I'm just following the books examples, this first chapter doesn't going into a lot of detail, and the source code they included doesn't follow the examples. I'm not exactly for sure how to do the multiple source files because I tried it that way. I understand how I did it in C++, Java not so much. Of course, I'm only in chapter 2.

Similar Threads

  1. Replies: 5
    Last Post: 11-13-2011, 04:58 PM
  2. Exception in Hello World Program in Hibernate
    By fierof2 in forum Advanced Java
    Replies: 4
    Last Post: 02-01-2011, 01:22 PM
  3. I have trouble understanding this program!
    By PureAwesomeness in forum New To Java
    Replies: 1
    Last Post: 03-16-2009, 06:34 AM
  4. Not able to debug simple hello world program
    By amit123solanki in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:41 PM
  5. trouble with program
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-03-2008, 10:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •