-2

How can i fix this code that it will work ?

  class Sample {
    public int a;
    private int b;
    int c;
    }
    public class ex {
    public static void main(String[] args) {
        Sample aClass = new Sample();
        aClass.a = 10;
        aClass.b = 10;
        aClass.c = 10;
    }
}

The error i got:

The public type ex must be defined in its own file
The field Sample.b is not visible

Z3RP
  • 328
  • 1
  • 16
  • can you provide the error message you got – Rahal Kanishka Oct 12 '18 at 05:52
  • 2
    `private int b;` - create a setter `setB(int b) {this.b = b;}` - or make in `public` see https://www.geeksforgeeks.org/variable-scope-in-java/ – Scary Wombat Oct 12 '18 at 05:52
  • You cant access private variable from another class. You can access the public variable from a different class. Also, you can access the variable with the default modifier (i.e int c) from a different class if the class is in the same package. Have a look at this thread. https://stackoverflow.com/questions/215497/what-is-the-difference-between-public-protected-package-private-and-private-in – Rans Oct 12 '18 at 05:58

6 Answers6

1

You can't access a Private varaible form outside of the class. What you have to do is change private int b; to public int b; or create a public setter method as in one of the comments pointed out.
For further clarification please refer this answer
Hope this helps.

Rahal Kanishka
  • 720
  • 13
  • 27
0

private members cannot be accessed outside of class directly.

Jignesh M. Khatri
  • 1,407
  • 1
  • 14
  • 22
0
class Sample {
    public int a;
    private int b;
    int c;
    public void setB(int b){
     this.b=b;
    }
   }
    public class ex {
    public static void main(String[] args) {
        Sample aClass = new Sample();
        aClass.a = 10;
        aClass.setB(10);//private variable not accessible directly
        aClass.c = 10;
    }
}
Mahamudul Hasan
  • 2,745
  • 2
  • 17
  • 26
0

Variable scope matters here. You can't access private variables out of the scope.

You have two options for this

  1. Make your variables public

    class Sample {
     public int a;
     public int b;
     int c;
    }
    
    public class ex {
      public static void main(String[] args) {
         Sample aClass = new Sample();
         aClass.a = 10;
         aClass.b = 10;
         aClass.c = 10;
      }
    }
    
  2. Keep your variables private and have getters and setters

    class Sample {
     public int a;
     private int b;
     int c;
    
     public int getB(){
       return this.b;
     }
     public void setB(int val){
       this.b = val;
     }
    }
    
    public class ex {
      public static void main(String[] args) {
         Sample aClass = new Sample();
         aClass.a = 10;
         aClass.setB(10);
         aClass.c = 10;
      }
    }
    
Ramesh
  • 2,297
  • 2
  • 20
  • 42
0

You have to follow best OOP practices for members. Always create a private members and make a public getters and setters.

public class Sample {

  private int a;
  private int b;
  private int c;

  public int getA() {
    return a;
  }

  public void setA(int a) {
    this.a = a;
  }

  public int getB() {
    return b;
  }

  public void setB(int b) {
    this.b = b;
  }

  public int getC() {
    return c;
  }

  public void setC(int c) {
    this.c = c;
  }
}
public class ex {
  public static void main(String[] args) {
    Sample aClass = new Sample();
    aClass.setA(10);
    aClass.setB(10);
    aClass.setC(10);
  }
}
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
0

The following concept will solve your problem

  1. public variable like a (you declared as public int a;) can be accessed using an object like obj.a (aClass.a = 10;)
  2. Private variable like b (you declared as private int b;) cannot be accessed directly using an object like obj.b. Private variable can be accessed only within the class i.e you need to write a setter to do that. Following is the complete code you can try to implement

    class Sample { public int a; private int b; int c;

     public void setb(int b){
     this.b=b;
     }
     }
      public class ex {
       public static void main(String[] args) {
        Sample aClass = new Sample();
        aClass.a = 10;
        aClass.setb(10);
        aClass.c = 10;
        }
        }
    
Bernard Nongpoh
  • 1,028
  • 11
  • 20