Posts

Showing posts with the label private

Try / catch with a private method [closed]

Try / catch with a private method [closed] I am trying to call a method that is private inherited from the super class (I KNOW THAT YOU CAN NOT CALL IT IN MAIN BECAUSE IT IS PRIVATE) However, I am trying to use a try catch statement that keeps running my program and give me an exception saying "This method cannot be Called because it is private" (EXAMPLE) this is what I have on my try / catch public int num1 = 3; try { superClass.methodOne(num1); } catch(Exception e) { System.out.printf("%s","ERROR: methodOne cannot be executed! IT IS PRIVATE"); } This is the error that it gives me! error: methodOne(int) has private access Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the que...

How to use class variables?

How to use class variables? A simple question. If I have class A with methods m1 and m2 . Is it a good practice that m1 changes the state of A . And then when m2 is called it relies on A 's state being changed by m1 . In other words if m2 is dependent on m1 being called before m2 being called. With m1 and m2 being both private. Worded differently is it O.K. for private methods to consider the object state as shared memory? Problem arises when they are called out of order. But with the benefit of not having to copy arguments. Any advice? 1 Answer 1 Your description is not a good design. Class variables should have class invariants that are true before and after any (and all) method invocations. The order of calls should not matter. The way you described it make is sounds that there will be cases during which this will not be true, for example if m1 is not called yet and m2 is. Moreover...