开发者

non-static method cannot be referenced in static context

开发者 https://www.devze.com 2023-03-11 12:50 出处:网络
I researched this around and still can\'t seem to solve this problem.I have the following in my program:

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 开发者_StackOverflowI 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


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.


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.


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.


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消