Vote Up 0 Vote Down

java program error message

I cant understand the error here.im a beginner.plz somebody explain.

class Ramu{
int k=128;
void m1(){
int p=27;
System.out.println("manish");
}
public static void main(String [] args){
Ramu r=new Ramu();
r.m1();
System.out.println(r.p);
System.out.println(r.k);
}
}
flag

Answers


Vote Up 0 Vote Down
we can declare a variable inside a method but the scope of that variable is inside that method.so
flag | link |
Vote Up 0 Vote Down
class Ramu{
private int k=128;
private int p;
void m1(){
this.p=27;
System.out.println("manish");
}
public static void main(String [] args){
Ramu r=new Ramu();
r.m1();
System.out.println(r.p);
System.out.println(r.k);
}
}
flag | link |
Vote Up 0 Vote Down
P is not member of class Ramu it is in method m1() which cannot be accessed directly........
flag | link |
Vote Up 0 Vote Down
Create a constructor for initializing your private int data member . You may also write a setter for it .
flag | link |
Vote Up 0 Vote Down
P is not visible outside the function
flag | link |

Your Answer

Login before answering

Login with facebook