java main去掉static_如何克服Java的Static Main()方法的局限性

I'm trying to work this out.

我正在努力解決這個問題。

In my project, i have a file called 'Hello.java' which is the file with the main() argument and which is called when the program is compiled. And I have another file called MyObj.java which has got just a random class I made up to test java's OO features. I'm trying to do this:

在我的項目中,我有一個名為'Hello.java'的文件,它是帶有main()參數的文件,在編譯程序時會調用它。我還有一個名為MyObj.java的文件,它只是一個隨機的類,我用來測試java的OO功能。我正在嘗試這樣做:

class Hello

{

public MyObj an_obj;

public static void main(String[] args)

{

setObj();

}

public void setObj()

{

this.an_obj.set_size(7);

say("size is " + this.an_obj.get_size());

}

}

In the MyObj.java class i have this code:

在MyObj.java類中,我有以下代碼:

public class MyObj

{

private int size;

public MyObj()

{

//do nothing

}

public void set_size(int new_size)

{

this.size=new_size;

}

public int get_size()

{

return this.size;

}

}

This however gives the error:

然而,這給出了錯誤:

"Cannot make a static reference to non-static method setObj() from the type Hello".

“無法從類型Hello”對靜態方法setObj()進行靜態引用。

If I add 'static' to the declaration of setObj, i.e

如果我在setObj的聲明中添加'static',即

public static void setObj()

Then I get:

然后我得到:

Cannot make a static reference to non-static field an_obj.

無法對非靜態字段an_obj進行靜態引用。

My question is, how can I accomplish what I'm doing, i.e setting and retreiving an object's field if the only way to start a program is with the Main method, and the main Method can only call static methods?? In what, how can I do anything at all with this limitation of being able to call static methods only?????

我的問題是,如果啟動程序的唯一方法是使用Main方法,並且main方法只能調用靜態方法,我怎樣才能完成我正在做的事情,即設置和檢索對象的字段?在什么,我怎么能做這個限制只能調用靜態方法?????

6 个解决方案

#1

You can either add "static" to the member variable as well, or instantiate the class from within your main method. Here is some example code for both approaches:

您也可以向成員變量添加“static”,或者在main方法中實例化該類。以下是兩種方法的示例代碼:

/* Static */

class Hello {

public static MyObj an_obj;

public static void main(String[] args) {

setObj();

}

public static void setObj() {

this.an_obj.set_size(7);

say("size is " + this.an_obj.get_size());

}

}

/* Non-Static */

class Hello {

public MyObj an_obj;

public static void main(String[] args) {

Hello hello = new Hello();

hello.setObj();

}

public void setObj() {

this.an_obj.set_size(7);

say("size is " + this.an_obj.get_size());

}

}

The primary difference between these two approaches is that in the first, "an_obj" is static--that is to say, there is only one variable for the entire program. If you were to instantiate multiple Hello objects, they would all have the same "an obj" reference. In the second, each Hello object has its own "an obj" reference, each of which can point to a different MyObj instance.

這兩種方法的主要區別在於,在第一種方法中,“an_obj”是靜態的 - 也就是說,整個程序只有一個變量。如果要實例化多個Hello對象,它們將具有相同的“obj”引用。在第二個中,每個Hello對象都有自己的“obj”引用,每個引用都可以指向不同的MyObj實例。

Obviously in this simple situation it doesn't make any difference one way or the other, but generally speaking, static members and methods should be avoided when possible.

顯然,在這種簡單的情況下,它不會以某種方式產生任何差異,但一般來說,應盡可能避免使用靜態成員和方法。

#2

Make both the setObj() method and the an_obj variable static, or do something like:

使setObj()方法和an_obj變量都是靜態的,或者執行以下操作:

public static void main(String[] args) {

new Hello().setObj();

}

#3

In addition to all previous answers I’d like to mention that only because you don’t understand a concept of a language that doesn’t necessarily constitute a “limitation”.

除了之前的所有答案之外,我還想提一下,只是因為你不理解一種語言概念並不一定構成“限制”。

#4

You never created an instance of MyObj.

您從未創建過MyObj的實例。

There are two logical places to create the necessary object.

創建必要對象有兩個邏輯位置。

Hello's initialization. public MyObj an_obj= new MyObj();

public static void main(String[] args)

{

setObj();

}

你好初始化。 public MyObj an_obj = new MyObj(); public static void main(String [] args) {     setObj(); }

Inside main() public MyObj an_obj;

public static void main(String[] args)

{

an_obj= new MyObj()

setObj();

}

在main()內部公開MyObj an_obj; public static void main(String [] args) {     an_obj = new MyObj()     setObj(); }

Either way, you have to actually create an object.

無論哪種方式,您都必須實際創建一個對象。

Additionally, you can make setObj static. However, this is rarely what you intend.

此外,您可以使setObj靜態。但是,這很少是你想要的。

The correct thing is for main to (1) create relevant objects, and (2) turn control over to those objects. This is what you should do.

正確的是main(1)創建相關對象,(2)將控制權交給這些對象。這是你應該做的。

class Hello

{

public MyObj an_obj= new MyObj();

public static void main(String[] args)

{

Hello aHelloObject= new Hello();

aHelloOject.setObj();

}

public void setObj()

{

this.an_obj.set_size(7);

say("size is " + this.an_obj.get_size());

}

}

#5

You can only call an instance method (i.e. a non-static method) when you have an instance of a class.

當您擁有類的實例時,您只能調用實例方法(即非靜態方法)。

#6

From a simple perspective, there are two different things in Java:

從簡單的角度來看,Java中有兩個不同的東西:

objects

static variables/functions

Objects get created at runtime, when you cause them to be created. Static things (all of them) are always there, and are loaded as needed.

當您創建對象時,會在運行時創建對象。靜態的東西(所有這些東西)總是在那里,並根據需要加載。

While you can mix and match these in your classes, you have to be careful how you use them. A static thing can call an object, but it must create a version of it first. An object, once created can call any static thing it likes.

雖然您可以在課程中混合使用這些內容,但您必須小心使用它們。靜態事物可以調用一個對象,但它必須首先創建它的一個版本。一個對象,一旦創建就可以調用它喜歡的任何靜態的東西。

We do tend to create classes that are objects AND have a few static bits as well, but you can have all static things in a class if you want, or you can just have all object-based things.

我們傾向於創建作為對象的類並且還有一些靜態位,但是如果需要,您可以在類中包含所有靜態內容,或者您​​可以只擁有所有基於對象的內容。

Static stuff is generally frowned upon, because it is essentially global, which opens up more possibilities for weird behavior. Objects with no static bits are best, and are usually thread safe if they are not calling things outside of there own internal data (make 95% of your code simple and safe, then deal with the complicate 5% in some special classes).

靜態的東西通常是不受歡迎的,因為它本質上是全局的,這為奇怪的行為開辟了更多的可能性。沒有靜態位的對象是最好的,並且如果它們不調用自己內部數據之外的東西,通常是線程安全的(使95%的代碼簡單而安全,然后在一些特殊類中處理復雜的5%)。

Paul.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值