Link to home
Start Free TrialLog in
Avatar of bmuralimohan
bmuralimohan

asked on

Calling private methods in main method

Hi,

Its a simple question, I got after coding for years.
Say we have a class like this.

<code>
public class Test
{
      private int x;
      private double test()
      {
           //Print some value
      }

      public static void main(String args[])
      {
            Test t = new Test();
            t.test();
            int y = t.x;
      }
}
</code>
I remember that, in my early years of Java coding, the above program used to give compillation error, becasue test() method is a private method and we are trying to call this on the instance ot Test.  Now I have tested this on Jre14.2 and this works fine I am really confused whether my understanding of Java is poor or my memory... Please claify me, whether this is a specification change in the newer versions of Java OR it used to be the same case even in older versions like Java1.1.6 etc.

Thanks and Regards
Murali
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sudhakar_koundinya
sudhakar_koundinya

Ignore my previous comment. I was thinking in terms of static
Sorry for wrong answering

Avatar of Mick Barry
> it used to be the same case even in older versions like Java1.1.6 etc.

:)
Mick,

I just a get the doubt like what Murali got.

Howcome instance.private variables can be accessed??
> Howcome instance.private variables can be accessed??

Cause its being accessed from a method of Test
This is guesswork but a good bet:

The java compiler gets better and better. It may even have become smart enough to look at your instance method and realize it does not actually depend on any particular instance

fx:
     public void writeHello( String name ) {   // this method is essentially static even if it is not declared as such
            System.out.println( "Hello " + name );
     }

but:
     public void writeMyNumber( ) {   // this method MUST be run in an instance of the class. it cannot be static
            System.out.println( "The number is " + this.x );
    }

mvh JakobA
class Test
{
     private int x;
     private double test()
     {
          //Print some value
              x=10;
              Test t=new Test();
              t.x=10;
              return 0D;
     }

     /*public static void main(String args[])
     {
          Test t = new Test();
          t.test();
          int y = t.x;
     }*/
}

class Test1
{
       public static void main(String args[])
     {
          Test t = new Test();
          t.test();
          int y = t.x;
     }
}
Seeing this difference in above class, I need to refresh my oops concepts once again :-)
I believe objects answer is correct. I can't ever remember a version of Java where this wouldn't have worked. The main method isn't magic. The only thing that makes it special is that the java interpreter recognizes it as the defined entry point to start application execution of a specified class. After that it has no special meaning. Since the main is a method in the Test class it has full access to private members and methods of an instance of the Test class.

That leads to an interesting but seemingly not well known aspect of private. The private access modifier is a class access specification, not an object access specification. So, in your example, if you have two instances of the Test class, they both have full access to private members of each other.

Regards,
Jim Cakalic
I tested the above code on 1.1.8 and it compiled fine.
It has to work on all versions ;-) it was probably a mere mistake where the asker tried to call it from a different class (like in Sudhakar's code) and it failed.
Hi, Murali. Did you still have a question?