java - How to access parent class integer variable in child class? -
this question has answer here:
i trying access int a= 10 variable in child class getting error:
cannot make static reference non-static field freshjuice.a
following code.
class freshjuice {     enum freshjuicesize{small,medium,large};     freshjuicesize size;     int   =   10; }  public class index extends freshjuice {      enum programminglanguage{php,java,dotnet,html};      public static void main(string[] args) {         // todo auto-generated method stub          system.out.println(freshjuice.freshjuicesize.small);         system.out.println(programminglanguage.php);         system.out.println(freshjuice.a); //getting error in line     }  }   i want directly access int variable of freshjuice class in child class. how can achieve target?
first create instance of freshjuice class:
freshjuice fj = new freshjuice();
now can access variable a .
but if set variable private int a=10;  can't access  still. that's why practice use getter , setter methods access private variables.
Comments
Post a Comment