2017-07-20 90 views
-1

這裏是我的問題:靜態初始化座返回爲void

public class Class5 
{ 
    static int ia, ib; 
    public static Class5 InstanceClass5 = new Class5(); 
    public Class5() 
    { 
     this(1, 2); 
    } 
    public Class5(int ia) 
    { 
     this(ia, 0); 
    } 
    public Class5(int ia, int ib) 
    { 
     this.ia = ia; 
     this.ib = ib; 
    } 
    // the only one important part of this class code 
    public static void returnConstructor(int ia, int ib) 
    { 
    } 
    // ends here, the rest isnt important 
    public static int myVar = initializeClassVariable(); 
    private static int initializeClassVariable() 
    { 
     int ia = 3; 
     return ia; 
    } 
} 

public class Main 
{ 
    static Scanner dataEntrance = new Scanner(System.in); 
    public Main() 
    { 
    } 
    public static Main Instancia1Main = new Main(); 
    static int ia, ib; 
    // here my problem begins: i just did 2 static blocks initializers, but its 
    // working good. 
    public static int varType = InitializeClassVariable(); 
    private static int InitializeClassVariable() 
    { 
     ia = Class5.ia; 
     return ia; 
    } 
    public static int varType2 = InitializeClassVariable2(); 
    private static int InitializeClassVariable2() 
    { 
     ib = Class5.ib; 
     return ib; 
    } 
    public static void main(String[] args) 
    { 
     System.out.println(Class3.Instancia2Class3.Class3()); 

     // It shows that the first static block initializer is doing what it was 
     // created to do: get the same value as "ia" class variable, 
     // inside Class5 (so, varType2 or InitializeClassVariable2 method is 
     // working by the same way, for sure) 
     System.out.println(Main.varType); 
     // final Main instancia1Main = Instancia1Main; -- it doenst have any 
     // importance for now 
     // AND FINALLY, THERES MY PROBLEM: inside Class5, returnConstructor 
     // method have 2 arguments: int ia, int ib 
     // To print this method values, i have to use 2 int arguments: why not 
     // varType and varType2 
     // or InitializeClassVariable and InitializeClassVariable2 (both options work by the 
     // same way... in theory) 
     // It supposed to work then... 
     System.out.println(Class5.InstanceClass5.returnConstructor(Main.InitializeClassVariable(), Main.varType2)); 
    } 
} 

Then it happens 我真的有不知道爲什麼returnConstructor正在接受上市參數(Main.InitializeClassVariable(),Main.varType2)爲void。 System.out.println(Main.varType); - >它表明,varType不是一個無效的,varType2肯定是一樣的,有人有任何想法,爲什麼它的發生和如何解決? 我想這就是所有,thx大家!

回答

1

returnConstructor不會返回任何東西,所以println沒有什麼可打印的。

+0

我剛開始使用java,不能相信我犯了這個錯誤。謝謝你pablochan – FARS