English 中文(简体)
主要类别中的每一种方法都不是固定的?
原标题:Does every method in main class have to be static?
  • 时间:2011-11-14 05:44:57
  •  标签:
  • java

在java,我只字不一,但在一夜之间,我看到,如果奥佩组织在主要类别中设计每一种方法,就必须是静态的吗? 在这项法典中,我无法在固定等级内使用某种方法。

似乎我不知你为什么会宣布某一类不变。 感谢你们的帮助!

public class JavaApplication2 {

private static CreateCar Vroom;
private static Limo Fuuu;

public static void main(String[] args) {
     Vroom = new CreateCar();
     Vroom.creator();
     getGas();
     addGas();
     getGas();
     Fuuu = new Limo();
     Fuuu.creator();
     Fuuu.wheels = 5;
     Fuuu.wheelie();
}
 public static int getGas(){
     Vroom.returnGas();
     return 0;
 }
 public static void addGas(){
     Vroom.fillerUp();
 } 
}
最佳回答

你可以称之为非统计方法,但你只能通过物体这样做。 这就是说,你需要把这种方法放在某个物体上。

Your main class can also be instantiated, so not every method in the main class needs to be static. For example:

public class MainClass {
    int value;

    public void printValue() {
        System.out.println("" + value);
    }

    public static void main(String[] args){
        MainClass o = new MainClass();
        o.printValue();
    }
}
问题回答

No. But the main method have to be static.

为在课堂上采用非静态方法,您必须创建<>参引<>>>>至的>目标>。 然后,你将这种方法从参考点上说。

public class JavaApplication2 {
    // non-static method
    public void go() {
        System.out.println("hello");
    }

    public static void main(String[] args) {
        // declare a reference
        JavaApplication2 app;
        // create an object
        app = new JavaApplication2();
        // call a method
        app.go();
    }
}

Short answer: yes.

原因是,贵格会需要援引“主”类别,这取决于是否已有过任何此类类型的事例。

细微的异构体:“主要”是某类成员(固定成员);它指该类人的名称。

有用年限:每一类别均可有OWN>/em>,DIFFERENT“main()”。 这对单位测试个别班级非常有用。

This sometimes comes as a shock to C/C++ programmers, where "main()" is associated with the ".exe", and you can only ever have one of them. In java, you can have many "main()" methods, and invoke whichever one you choose at runtime.

[......],你没有“法定主人”班。 页: 1

没有要求在该类别中采用任何其他固定方法。 为了使用这一类别,请立即:

public static void main(String[] args) 
{
    JavaApplication2 myApp = new JavaApplication2();
    myApp.someMethod(); 
    ...
}

main is a specifically named static method that acts as an entry point. When you tell Java to "run" your program from the command line like:

java JavaApplication2

履历表

答案很短:没有,每一种方法都不需要固定。

事实上:

  1. for small exercises, it is best to not have a different class for your main method.
  2. you can have multiple classes with main methods (pretty common in many libraries, for testing or demonstrating the use of the class)
  3. when you invoke a java program, which is typically a jar file, you have a choice of which class s main method you want to invoke
  4. as a beginner try to avoid static method unless you are sure you need them. this is because somebody not good at object oriented programming can easily abuse static methods to do function-oriented programming (not to be confused with functional programming) by using static methods.
  5. a main method (note, i am not saying "the" main method) must be static: public static void main (String [] argv) { /* ... */ }

法定方法意味着你可以直接通过这一方法使用。

public class ClassName{
    //no-op
    public static void method(){//....}
}    
ClassName.method();

在某些情况下,可删除<代码>ClassName。

static import

main method

但是,为了援引非法定方法,你必须首先提出。

method is declared as static, so as to be referred by class name... eg

Math.pow(x,y)  // x^y

在这种方法中,用户不必担心创建案例。 因此,采用静态方法的基本原因......。

静态方法可指非静态方法,但以类别标的。

ClassName cn =new ClassNam().methodName;

main() is special method, its starting point of execution of the program, so u can never have a object without running the program, and hence its called like(execution:: java ClassName), so it must be static...





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...