调用构造函数时的非静态变量

时间:2019-04-02 06:48:28

标签: java

我正在处理我的代码,以从我创建的类中调用一些构造函数。

很抱歉,我的问题很简单,但是我尝试到处搜索并且没有找到答案。

另一方面,我所有的课程都运行良好,没有任何错误。

当我需要从主类调用构造函数时,这给了我一个错误。

  

非静态变量,不能从静态上下文中引用。

我将发布我正在呼叫的第一堂课,并且出现错误。

public class Test1 {    

    static int nextPers = 1;

    public class Person{
    private int persID;
    private String persName;
    private String email;

    Person(int persID, String persName, String email){
        persID = nextPers;
        nextPers++;
        this.persName = persName;
        this.email = email;
    }


public static void main(String [] args){
    Person per = new Person(1 , "Raphael" , "meh@hotmail.com");
   }
}

我无法继续,程序正在要求我放置static变量,而且我也无法弄清楚我的错误,因为当我在变量之前写入static时,它会创建另一个错误。

4 个答案:

答案 0 :(得分:3)

您的课程PersonTest1的内部课程。

将其移动到其自己的文件或添加static关键字,如下所示:

public class Test1 {
    public static class Person {
        // your fields
    }
}

我建议阅读other answer以获取详细说明。

答案 1 :(得分:0)

nextPers必须是静态类成员,例如

public class Person {
    private static int nextPers = 0;

    private int persID;
    private String persName;
    private String email;

    Person(String persName, String email) {
        this.persID = nextPers++;
        this.persName = persName;
        this.email = email;
    }
}

(在您的原始代码中,您将nextPers分配给了persId参数,我认为这不是您真正想要的参数,因为它用相同的名称掩盖了您的实例变量。)

答案 2 :(得分:0)

您应该有一个静态的ID计数器,不要将其提供给构造函数

public class Person {
    private int persID;
    private String persName;
    private String email;
    private static int nextPers = 0;

    Person(String persName, String email){
        this.persID = nextPers;
        nextPers++;
        this.persName = persName;
        this.email = email;
    }

    public static void main(String [] args){
        Person per = new Person("P0" , "p0@hotmail.com");
        System.out.println(per.persID + " " + per.email + " " + per.persName);
        per = new Person("P1" , "p1@hotmail.com");
        System.out.println(per.persID + " " + per.email + " " + per.persName);
        per = new Person("P2" , "p2@hotmail.com");
        System.out.println(per.persID + " " + per.email + " " + per.persName);
    }
}

答案 3 :(得分:0)

Person类放在外面,不需要persId进入构造函数。

package com.test;

class Person{
   private int persID;
   private String persName;
   private String email;

   Person( String persName, String email){
       this.persID = Test.nextPers;// Whenever your constructor is invoked it will assign nextPers to persId
       this.persName = persName;
       this.email = email;
       Test.nextPers++;// finally increment here
   }

   public int getPersID()
   {
      return persID;
   }

   public void setPersID( int persID )
   {
      this.persID = persID;
   }

   public String getPersName()
   {
      return persName;
   }

   public void setPersName( String persName )
   {
      this.persName = persName;
   }

   public String getEmail()
   {
      return email;
   }

   public void setEmail( String email )
   {
      this.email = email;
   }

   @Override
   public String toString()
   {
      return "Person [persID=" + persID + ", persName=" + persName + ", email=" + email + "]";
   }
}
public class Test
{
   static int nextPers = 1;

   public static void main( String[] args )
   {
      Person person1 = new Person("Raphael" , "meh@hotmail.com");
      System.out.println( person1 );
      Person person2 = new Person("Man" , "man@hotmail.com");
      System.out.println( person2 );
   }
}

输出

Person [persID=1, persName=Raphael, email=meh@hotmail.com]
Person [persID=2, persName=Man, email=man@hotmail.com]