0

I researched this around and still can't seem to solve this problem. I have the following in my program:

   public void InsertDB(double price, double shares, String ticker, int id)throws Exception {
   //do some stuff
   }

I tried the following within my main class(and got the above error non-static method cannot be referenced in static context):

InsertDB(constants[i], variables[i], ticker[i], count);

Then I read you must create a new instance so I tried(Testingground is the name of my program) and I get an error saying it cannot find the InsertDB symbol:

Testingground myObject = new InsertDB();

I'm new to java and kind of inherited this program(I haven't had this problem with my programs), can someone tell me what I can do to get this to work and the logic behind it?(my insertdb class gives me errors if I turn it to static so that approach won't work). I also read that it might work if I changed public to protected but it still didn't work.

Thanks in advance

Lostsoul
  • 25,013
  • 48
  • 144
  • 239

5 Answers5

5

You need to instantiate the class itself, not the method InsertDB(). You instantiate a class by using the key word new. You can refer to the official tutorial on the oracle/sun docs.

Example:

Testingground myObject = new Testingground();
myObject.InsertDB(constants[i], variables[i], ticker[i], count);

Methods can also be declared as static. In that case you can reference them directly ie. Testingground.InsertDB(constants[i], variables[i], ticker[i], count); without constructing an object for that class.

Also by convention method names are camel cased. Read more about naming conventions in java here.

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
  • This maybe the conflict but when I do that I get an error: constructor Testingground in class testingground.Testingground cannot be applied to given types; required: testingground.Testingground.SolverResult found: no arguments I have another class called SolverResult within the application – Lostsoul Jun 07 '11 at 19:47
  • should I create a new class, instead of Testingground..something else and put the class to upload to the DB under there? – Lostsoul Jun 07 '11 at 19:49
  • @Lostsoul - your `Testingground` class must have a constructor defined that takes `SolverResult` as an argument. Can you add that constructor snippet in your post? – CoolBeans Jun 07 '11 at 19:54
  • Is this what your asking for: public class Testingground { private SolverResult delegate; – Lostsoul Jun 07 '11 at 19:59
  • @Lostsoul - I am looking for the constructor method. It could be something like this:- `public Testingground(SolverResult delegate){this.delegate=delegate;}` – CoolBeans Jun 07 '11 at 20:02
  • opps sorry, here it is: public Testingground(SolverResult delegate) { this.delegate = delegate; } public SolverResult getDelegate() { return delegate; } – Lostsoul Jun 07 '11 at 20:03
  • then there's this: static public interface SolverResult { public void solution(double[] constants, double[] variables, double total, String[] ticker); } – Lostsoul Jun 07 '11 at 20:03
  • @Lostsoul - SolverResult is an inteface per your example. Do you know of a class that implements it? – CoolBeans Jun 07 '11 at 20:10
  • I think its this: Testingground solver = new Testingground(new SolverResult() { int count = 1; @Override public void solution(double[] constants, double[] variables, double total, String[] ticker) { //blah blah blah //try to insert data into database InsertDB myObject = new InsertDB(); myObject.InsertDB(constants[i], variables[i], ticker[i], count); – Lostsoul Jun 07 '11 at 20:23
  • I am actually getting a different error now, I tried to take some of the advice below and created a new class file and put it in there(called it InsertDB as above). but now I get exceptions must be caught to be thrown – Lostsoul Jun 07 '11 at 20:24
  • if I add throws Exception to the above implemention, I get "solution(double[],double[],double,java.lang.String[]) in cannot implement solution(double[],double[],double,java.lang.String[]) in testingground.Testingground.SolverResult overridden method does not throw java.lang.Exception" – Lostsoul Jun 07 '11 at 20:27
  • Thanks CoolBeans..it works now. I just added "throws Exception" to everything until I stopped getting errors. Is there any downside of adding throws Exception to code? – Lostsoul Jun 07 '11 at 20:36
  • @Lostsoul - you are actually not on the right track here. You need to first find a class that implements `SolverResult`. You should see a class that looks something like this - `public final class SolverResultImpl implements SolverResult` ... – CoolBeans Jun 07 '11 at 20:37
  • I found it and then I added throws Exception. here it is: private void solveImpl(double[] c, double[] v, double t, int n, double r, String[] ticker) throws Exception { – Lostsoul Jun 07 '11 at 20:46
  • @Lostsoul - Good deal. You should catch the thrown exception upstream to find out the exact exception. – CoolBeans Jun 07 '11 at 20:51
  • Thanks so much. I tested it and it worked perfectly..Thanks a million CoolBeans – Lostsoul Jun 08 '11 at 04:05
2

You need to create an instance of the class that contains the InsertDB method, and then invoke the method on the instance.

Alternatively, you could make the method static, and then you would not need an instance from which you invoke it.

You need to understand that static fields/methods are defined on the class itself. So there is one instance of a static field/method in the entire JVM. Non-static methods/fields live on the instances of the objects -- each object has its own non-static field/method.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
1

You can't instantiate the method name. You have to instantiate the class that the method is in, and then call the method on your object.

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
1

You are trying to declare a method as a class. In java, the methods are supported within their class containers, which can be declared as objects. If your InsertDB() function was in a class (for example, DataManager.java, declared as public class DataManager), you would do something like this:

DataManager dmanager = new DataManager();
dmanager.InsertDB(constants[i], variables[i], ticker[i], count);
John Leehey
  • 22,052
  • 8
  • 61
  • 88
  • Thanks John Right now I'm getting this error now: constructor Testingground in class testingground.Testingground cannot be applied to given types; required: testingground.Testingground.SolverResult found: no arguments I have another class called SolverResult within the application as mentioned I'm new to java, is it more advisable to create a new file and create a new class for this function? – Lostsoul Jun 07 '11 at 19:52
  • You're going to have to post your code, but from what it sounds like, you're trying to declare a Testingground object with a constructor that doesn't exist. There's so many things that can be wrong though, its impossible to tell you what each compiler error means without looking at the code itself. – John Leehey Jun 07 '11 at 20:04
1

A non static method need an object reference (this) to be executed. Static method can be called without an allocated object. So referencing a field type or a class method from inside a static method is impossible, because there isn't an instance of the object on which call the required method.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190