-1

I'm working on my code, to call some constructors from classes I created.

I'm sorry if my question is easy, but I tried to search everywhere and didn't figure out the answer.

On the other hand, all my classes are working well without any error.

When I need to call the constructor from the main class, it's giving me an error.

non-static variable this cannot be referred from a static context.

I will just post the first class I'm calling and having an error.

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

I cannot continue, the program is asking me to put static variables, and I cannot figure my error because when I write static before the variable, it's creating another error.

  • why do you have a nextPers++; statement while you don't have a nextPers static variable? Also: persID = nextPers; should be this.persID = persID; – Stultuske Apr 02 '19 at 06:49
  • 2
    What is `nextPers` ? – Sudhir Ojha Apr 02 '19 at 06:50
  • Im putting it before the class Person sir. @Stultuske – Raphael Eid Apr 02 '19 at 06:50
  • 1
    You shouldn't have side effects in constructor – Andronicus Apr 02 '19 at 06:50
  • ```public class Test1 { static int nextPers = 1; public class Person{ ``` ... – Raphael Eid Apr 02 '19 at 06:50
  • "before the class" that makes no sense. static variables still have to be inside the class – Stultuske Apr 02 '19 at 06:50
  • 1
    `persID = nextPers;` should probably be `this.persID = nextPers;`, but then why have a `persID` argument in your constructor. – Eran Apr 02 '19 at 06:51
  • I don't know what's happening sir, all my classes are working well without any error, when i'm calling the constructor from main it's giving it to me. Any Idea? – Raphael Eid Apr 02 '19 at 06:51
  • 2
    @RaphaelEid several, but they've been mentioned already. if you have a nextPers variable, remove the persID parameter, and make sure you actually have a nextPers variable, which at current, you don't. – Stultuske Apr 02 '19 at 06:52
  • 1
    @RaphaelEid Is the `class Person` an inner class? – Lino Apr 02 '19 at 06:52
  • 5
    It looks like your `Person` class is an inner class of your `Test1` class. That's probably the cause of the error. Either move `Person` class outside of make it static. – Eran Apr 02 '19 at 06:52
  • No sir, I don't have inner classes, it's all independent @Lino – Raphael Eid Apr 02 '19 at 06:53
  • @Stultuske I need to put a counter which is static variable, and then in this counter i need to increment 1 each time and give it as an id to a person. – Raphael Eid Apr 02 '19 at 06:54
  • 1
    @RaphaelEid Please, provide a [mcve], don't include code in the comments, but rather [edit] your post with the least amount of information needed to understand, reproduce and possibly fix your problem – Lino Apr 02 '19 at 06:56
  • 1
    @Lino Okay sir I will do it right now. – Raphael Eid Apr 02 '19 at 06:56
  • We must be missing some information here; the code provided does not reproduce the error: https://ideone.com/ktTE2C . Note that I had to add a declaration of `nextPers` to make it compile - so maybe the problem is in your declaration of `nextPers` ? – S.L. Barth is on codidact.com Apr 02 '19 at 06:57
  • 2
    Based on your comment - `public class Test1 { static int nextPers = 1; public class Person{` - `Person` class is definitely an inner class. @RaphaelEid – Eran Apr 02 '19 at 06:58
  • @RaphaelEid then you still actually need to add a static counter, which you didn't do. you are using a non-existent counter. That's my point. In a previous comment you state Person is NOT an inner class. Your edited code contradicts that. – Stultuske Apr 02 '19 at 07:00
  • @S.L.Barth but sir i need to make the counter static not private – Raphael Eid Apr 02 '19 at 07:01
  • 1
    It makes no sense to have the `Person` class as an inner class in `Test1` if `Person` class is the one you are going to test and you shouldn't make your tested class be dependent of the test class. – Joakim Danielson Apr 02 '19 at 07:03

4 Answers4

3

Your class Person is an inner class of Test1.

Move it to its own file or add the static keyword, like so:

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

I suggest reading up this other answer for a detailed explanation.

Lino
  • 19,604
  • 6
  • 47
  • 65
  • 3
    And to explain the error message, you cannot create an inner class instance without an outer class instance, that is why it looked for `this` (which you do not have in `static void main`) – Thilo Apr 02 '19 at 07:02
  • But i didn't want to create an inner class, how java understands alone that this class ```Person``` is an inner class of ```Test1``` even if I didn't use ```extends``` in other classes below. – Raphael Eid Apr 02 '19 at 07:04
  • 1
    inner class has nothing to do with extend – Joakim Danielson Apr 02 '19 at 07:05
  • 1
    @RaphaelEid an inner class is **not** a subclass, an inner class is defined that it has an outer enclosing class, like the example you've shown. `Test1` is *around* `Person` – Lino Apr 02 '19 at 07:05
0

nextPers must be a static class member, like

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

(In your original code you are assigning nextPers to the persId argument, which I assume isn't what you really want, as it masks your instance variable with the same name.)

AndreasPizsa
  • 1,736
  • 19
  • 26
0

You should have a static counter for ID and not give it into constructor

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);
    }
}
Cray
  • 2,774
  • 7
  • 22
  • 32
  • Thank you sir, but why we shouldn't put the counter or ```personID``` as a parameter. So maybe while he puts his id in the main, it would have an error with the counter? For example he used ```Person per = new Person (55, "p0" , "p0@h.c"); ``` Then the counter automatically will increment ```1``` And the user told him that he wants his id to be ```55```, this would makes an error with the counter right? – Raphael Eid Apr 02 '19 at 07:08
  • Because you overwrite the inserted value in your code anyways, if you want to assign id using inserted value you can do a second constructor where `this.persID = persID` – Cray Apr 02 '19 at 07:23
0

Place your Person class outside and you do not need to persId into your constructor.

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

Output

Person [persID=1, persName=Raphael, email=meh@hotmail.com]
Person [persID=2, persName=Man, email=man@hotmail.com]
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24