Results 1 to 4 of 4
  1. #1
    RyanSL is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Help with Static Conscructors

    I have a problem in a program I'm working on were I'm getting a error that states that my constructor is static

    Java Code:
    public class JavaForumsName extends JPanel implements ActionListener {
        class CreateGrid
        {
            private boolean drawImage;
            CreateGrid (File file) throws IOException
            {
                BufferedImage img = null;
                img = ImageIO.read(file);
                drawImage = Graphics.drawImage(img, 0, 0, null);
            }
        }
    }
    Any help is appreciated. Thank you :)
    Last edited by RyanSL; 04-22-2011 at 02:48 PM. Reason: Correcting class names to Java standards

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Why are you creating createGrid (CreateGrid would be a better name) as an inner class of JavaForumsName?

    I'm getting a error that states that my constructor is static

    What are the exact compiler messages?

  3. #3
    RyanSL is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Error Message

    Error Code:

    Error non-static method drawImage(java.awt.Image,int,int,java.awt.image.Im ageObserver) cannot be referenced from a static context JavaForumsName.java 129 .../src/components

    I changed createGrid to CreateGrid (I forget about coding standards from time to time).

    I created "CreateGrid" to access a static function.
    Last edited by RyanSL; 04-22-2011 at 02:51 PM. Reason: Adding more text

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    The error doesn't state your constructor is static, but a method you use in your constructor is NOT static, and you are trying to use it in a static way. drawImage is not static, so don't use it as such. If you want to load images, see Lesson: Working with Images (The Java™ Tutorials > 2D Graphics)

Similar Threads

  1. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  2. Replies: 4
    Last Post: 05-12-2010, 12:05 PM
  3. Replies: 3
    Last Post: 02-11-2010, 10:59 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

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