Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Newb Java Problem - non-static method cannot be accessed from static context

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Newb Java Problem - non-static method cannot be accessed from static context

    Any Java Gurus out there that can help a newbie?

    I'm trying to create a program to write a png and jpeg file from an image I create. I'm getting the usual "non-static method cannot be accessed by static context" error. I know the general explanation is that I'm trying to access a method without creating a new instance of it. However, I thought one of my alternate solutions would solve the problem - but they don''t.

    Here's the code:

    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.awt.Graphics2D;
    import java.awt.image.RenderedImage;
    import javax.imageio.ImageIO;
    import java.awt.Color;
     
     
    public class WriteText_PicTest {
     
        public RenderedImage myCreateImage()
            {
            int width = 100; int height = 100;
     
            // Create a buffered image in which to draw
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
     
            // Create a graphics contents on the buffered image
            Graphics2D g2d = bufferedImage.createGraphics();
     
            // Draw graphics
     
            g2d.setColor(Color.white);
            g2d.fillRect(0, 0, width, height);
            g2d.setColor(Color.black);
            g2d.fillOval(0, 0, width, height);
     
            // Graphics context no longer needed so dispose it
            g2d.dispose();
            return bufferedImage;
        }
     
            public static void main (String args[])
        {
     
            // Create an image to save
     
    RenderedImage rendImage = myCreateImage();
     
    // Write generated image to a file
     
            try {
                // Save as PNG File
                File file = new File("newimage.png");
                ImageIO.write(rendImage, "png", file);
     
                // Save as JPEG
                file = new File("newimage.jpg");
                ImageIO.write(rendImage, "jpg", file);
     
            } catch (IOException e) { }
        }
     
        // Returns a generated image.
     
     
    }

    Here's what I've tried (and what has failed). It says it cannot find the symbol myCreateImage

    changing:
    RenderedImage rendImage = myCreateImage();

    to

    RenderedImage rendImage = new myCreateImage();

    or

    RenderedImage rendImage = new rendImage.myCreateImage();

    or

    RenderedImage rendImage = rendImage.myCreateImage();

    Any help anyone could give me (especially an explantion why a new instance isn't created) would be appreciated.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Newb Java Problem - non-static method cannot be accessed from static context

    I'm guessing that the problem is that myCreateImage() isn't a static method.

    public static RenderedImage myCreateImage()
    {
        //... code
    }

    Then your first method should work, or if you're accessing it from a different class, use the class name and access that method:

    // works with limitations
    RenderedImage rendImage = myCreateImage();
    // works all the time
    RenderedImage rendImage = WriteText_PicTest.myCreateImage();

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    TedVon (September 5th, 2010)

  4. #3
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Newb Java Problem - non-static method cannot be accessed from static context

    Last edited by Brt93yoda; September 5th, 2010 at 04:31 PM.

  5. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Newb Java Problem - non-static method cannot be accessed from static context

    That did the trick, HelloWorld.

    Do you happen to know why the method needs to be static?

  6. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Newb Java Problem - non-static method cannot be accessed from static context

    Do you happen to know why the method needs to be static?
    That's why I posted a link.

  7. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Newb Java Problem - non-static method cannot be accessed from static context

    Man, that is the one problem that almost everyone has when they start Java. It is a very confusing error when you first start. It took me forever to even understand what it meant when I first started.

  8. #7
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Newb Java Problem - non-static method cannot be accessed from static context

    I've never had the problem, but I was taught that.

  9. #8
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Newb Java Problem - non-static method cannot be accessed from static context

    Sorry BRT93! Let me know if this is should be asked in the Java theory forum.

    I followed the links and have a couple of questions to make sure I understand it correctly.

    Do all methods within a Class with a "main" block have to be static? And, any methods created as part of other objects do not need to be static. However, if an instance of that object is created in the main block the method is accessed by fully qualifying the reference (e.g. Bicycles.numberOfBicycles).

    If so, why can't a main directly reference a method within it's own class? Is it just one of those java things?

  10. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Newb Java Problem - non-static method cannot be accessed from static context

    All methods and variables referenced inside a static method MUST be static if declared outside that static method.

    Here are some examples:
    Referencing Methods
    All methods referenced from inside a static method must be static. So, since we call method1() inside our main, it has to be static. BUT, since we call method2() inside method1(), method2() must be static aswell because method1() is static.
    public static void main(String[] args)
    {
         method1();
    }
    public static void method1()
    {
         method2();
    }
    public static void method2()
    {
     
    }

    Referencing Variables
    Since we are referencing variable1 inside our main, it has to be declared static. Since we are also referencing method1() in our main, it means method1() needs to be static. And since we are referencing variable2 inside method1(), which is static, it means that variable2 needs to be static. But, since we are creating variable3 inside our static method1(), it does not need to be static because the scope limits it to just that method and cannot be referenced by any other method or location.
    public test
    {
    	public static int variable1=0;
    	public static int variable2=0;
     
    	public static void main(String[] args)
    	{
    		System.out.println(variable1);
    		method1();
    	}
     
    	public static void method1()
    	{
    		int variable3 = 0;
    		System.out.println(variable2);
    		System.out.println(variable3);
    	}
    }

    Tell me if that clears things up.
    Last edited by aussiemcgr; September 6th, 2010 at 01:49 PM.

  11. #10
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Newb Java Problem - non-static method cannot be accessed from static context

    AUSSIEMCGR - thank you so much. I'm sure I read that somewhere, but it didn't sink in until you explained it. Thanks again.

    i'm going to do some reading and I might be back. I'm trying to figure out when a method wouldn't have to be static.

    Thanks again!

  12. #11
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Newb Java Problem - non-static method cannot be accessed from static context

    @TedVon, sorry that I didn't respond. I was on a road trip. Thank you very much AussieMCGR for finishing the topic.

Similar Threads

  1. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 02:24 AM
  2. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 03:36 AM
  3. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 12:56 PM
  4. Static method
    By kalees in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2009, 10:10 AM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 04:24 PM