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 2 of 2

Thread: Cannot use "this" in a static context

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Location
    Netherlands
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot use "this" in a static context

    Hello all, I am new to JAVA programming, so I started a few days ago with my first program.

    I found an example at YouTube with the code attached below.

    However, the following message appears:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Cannot use "this" in a static context

    I have searched on the internet, but unfortunately without any success.

    If I change "this" to "null", then I can run the program , however nothing happens if I hit the button.

    Any idea where I should find this problem?

    Thanks in advance.

    button.addActionListener(this);

    public class Main implements ActionListener {	
     
    public static void main(String[] args) {
     
    		int top=500, left=130, bottom=130, right=500;
     
    		JFrame frame = new JFrame();
    		JButton button = new JButton("Click me");
    		button.addActionListener(this);
     
                    JLabel label = new JLabel("Number of clicks");			
    		JPanel panel = new JPanel();
     
    		panel.setBorder(BorderFactory.createEmptyBorder(top, left, bottom, right));
    		panel.setLayout(new GridLayout(0,1));
    		panel.add(button);
      		panel.add(label);
     
    		frame.add(panel, BorderLayout.CENTER);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setTitle("Title");
    		frame.pack();
    		frame.setVisible(true);		
     
     
    	}   // End Static Main

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cannot use "this" in a static context

    Cannot use "this" in a static context
    this refers to the current instance of the class the statement using this is in. A static context is not in an instance of the class.

    If you want to use this, create an instance of the class where the statement is located.
    Change the main method to create an instance of the class and add a constructor. Move the code that is in the main method to the constructor.
    For example:
       ... main(String[] args) {
        new TheClass();
     } // end main method
    public TheClass() { //  start constructor
     ...// code from main method here
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: March 3rd, 2019, 08:44 PM
  2. [SOLVED] Need help desperately: "non-static variable this cannot be referenced from a static context"
    By mikbferguson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2017, 08:27 PM
  3. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  4. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  5. Why is there no "package-private interfaces" and "abstract static"?
    By Cornix in forum Java Theory & Questions
    Replies: 12
    Last Post: September 14th, 2013, 11:20 AM