• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

static vs. non-static

I am having the hardest time getting this to work.  I keep getting that "non-static method cannot be referenced from a static context" error, and I've tried creating an object to call it, but that isn't working either (it gets rid of the above error, but doesn't work for what I want to do)

so I created this class that doesn't say "static" anywhere in it:

package semantic.shum;

import java.awt.event.*;
import java.util.*;
import java.io.*;

public class find3 {
   
 public void find3(WindowEvent e) {
     semantic.shum.Semantic.tool_dummy_button.doClick();
 }

}

but i still get the "cannot be referenced from a static context" error.  What makes the above code a "static context"?  How do I make it non-static?
0
swp3h6
Asked:
swp3h6
3 Solutions
 
CEHJCommented:
Buttons and their events seldom exist in a static context, so doClick is probably *not* static
0
 
CEHJCommented:
0
 
swp3h6Author Commented:
"Buttons and their events seldom exist in a static context, so doClick is probably *not* static"
none of this should be static...

but the line:
semantic.shum.Semantic.tool_dummy_button.doClick();

still gets the error:

non-static variable tool_dummy_button cannot be referenced from a static context.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CEHJCommented:
That's because 'tool_dummy_button' is an *instance* variable of semantic.shum.Semantic, not a class (static) one
0
 
AstaedusCommented:
new semantic.shum.Semantic.tool_dummy_button().doClick();
this works because here you make an instance of the class semantic.shum.Semantic.tool_dummy_button

semantic.shum.Semantic.tool_dummy_button is the name of a class.
only static methods can be used with a class.
If you want to use other methods you need a reference to an instance of that class.

It's basic java:
you're trying to do this:
MyClass.doSomething();
when you need:
MyClass myInstance;
myInstance = new MyClass();
myInstance.doSomething();

The only way you can do MyClass.doSomething(); is by making the doSomething method static.
0
 
objectsCommented:
try:

public class find3 {
     
 public void find3(WindowEvent e) {
     new semantic.shum.Semantic().tool_dummy_button.doClick();
 }

}

or instead of creating an instance each time, make it a member var:

public class find3 {
      private semantic.shum.Semantic() semantic = new semantic.shum.Semantic();

 public void find3(WindowEvent e) {
     semantic.tool_dummy_button.doClick();
 }

}

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now