-1
public static void main(String[] args)
{
  Clock c = new Clock();
  c.m = c.m + 1;
  //where m is a private instance variable in an another class.`
}

Why is it incorrect for the main() method using a class Clock to have the second statement below? Please help?

sKhan
  • 9,694
  • 16
  • 55
  • 53
  • What are you trying to do ? – YounesM Feb 15 '16 at 01:07
  • you can do one thing generate setter getter for m instance variable on clock class and from main method call using c.getm(); that will give the data of m . since private variable you can not access out side of the class that's why use setter getter concept – Anand Dwivedi Feb 15 '16 at 01:16
  • What did the compiler tell you when you tried to compile this? – Erwin Bolwidt Feb 17 '16 at 01:45

2 Answers2

0

You can't access private members outside of the class https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Nenad
  • 484
  • 3
  • 14
0

You cannot access a private instance outside a class.It is local to the class it is declared in.If you want to do so, make it public, then you can access it using the class object.

Adnan Temur
  • 333
  • 1
  • 10