Java Constructor variables being ignored

前端 未结 7 1011
我在风中等你
我在风中等你 2021-01-27 05:54

I am trying to create a instance of the object Iset. When the person makes the object they have to give an int which will be the size of a boolean array that will store a set of

相关标签:
7条回答
  • 2021-01-27 06:16

    size variable in your constructor is a local variable, that's why other member methods doesn't get the right size to check.

    Assign the value to this.size, then it will work:

    ISet(int a) {
            this.size = a;
            seti = new boolean[size];
    
    0 讨论(0)
  • 2021-01-27 06:18

    You should use the this keyword, an make the constructor public if you want to instantiate it in another class:

    public class ISet {
        public int size;
        boolean[] iSet;
    
    
        public ISet(int a) {
            this.size = a;
            this.iSet = new boolean[a];
        }
    
    0 讨论(0)
  • 2021-01-27 06:19

    You are creating a new variable inside your constructor and this is called shadowing. Use this to set the attributes of the current object:

    this.size = a;
    
    0 讨论(0)
  • 2021-01-27 06:20

    You're creating a new int variable inside the constructor. Instead, you just need to do this.size = a; in the constructor.

    0 讨论(0)
  • 2021-01-27 06:21

    You are not initializing the size instance variable properly, instead you are initializing a local variable size. Therefore your size instance variable remained initialized with 0 and your seti instance variable is an empty array causing the out of range error.

    As pointed out by others, you don't need the instance variable size.
    There is also no need for another local variable size inside your constructor, Just use seti.length to determine the size of the array. To simplify, your code should be:

    public class Iset {
    boolean[] seti;
    
    ISet(int a) {
        seti = new boolean[a];
    

    I would recommend you use static analysis tools like checkstyle to eliminate bug like this in your codes.

    0 讨论(0)
  • 2021-01-27 06:28

    Lets have a look at your code :

     public class Iset {
        public int size;// Declares a Member of a class and all the objects will have a copy of this member
        boolean[] Iset;
    .....
    }
    
    
        ISet(int a) {
            int size = a; //This line is declaring a **local variable** called size 
            seti = new boolean[size];
    ...
    }
    

    See in your constructor you've created a local variable size but you also have a class member called size in your class. So in this scenario whenever we try to set size variable within constructor, there arises a conflict to the compiler whether to set the local variable or the class member ( this conflict is because of the fact that both the local variable and class member has same name ) . In such scenarios the compiler chooses local variable size over the class member size. But for you to make sure that the values you use in the constructor are used in all of my methods, you have to set the class member. To set the class member we use following code:

    this.size = a;//Says set the object member size to value present in a.
    

    Here we refer the size using this pointer because we need to set the object's size variable and not the local variable size.

    0 讨论(0)
提交回复
热议问题