• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

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
0
bmuralimohan
Asked:
bmuralimohan
  • 5
  • 3
  • 2
  • +2
1 Solution
 
sudhakar_koundinyaCommented:
that never raises the problem in  older and newer version of Java

previously you might have done the code some thing like this. which is obviously raise the compile time exceptions


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

     public static void main(String args[])
     {
          test();
          int y = x;
     }
}


in order to access the provate functions in main with out an instance varaible you should do some thing like this

The reason is to call methods/varaibles  in a static method, that methods/variables should be either static or should be called from an instance.

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

     public static void main(String args[])
     {
          test();
          int y = x;
     }
}


Hope you understand. If not let me know. I will try to clarify more

Regards
Sudhakar
0
 
sudhakar_koundinyaCommented:
Ignore my previous comment. I was thinking in terms of static
Sorry for wrong answering

0
 
objectsCommented:
> it used to be the same case even in older versions like Java1.1.6 etc.

:)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sudhakar_koundinyaCommented:
Mick,

I just a get the doubt like what Murali got.

Howcome instance.private variables can be accessed??
0
 
objectsCommented:
> Howcome instance.private variables can be accessed??

Cause its being accessed from a method of Test
0
 
JakobACommented:
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
0
 
sudhakar_koundinyaCommented:
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;
     }
}
0
 
sudhakar_koundinyaCommented:
Seeing this difference in above class, I need to refresh my oops concepts once again :-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
:)
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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
0
 
objectsCommented:
I tested the above code on 1.1.8 and it compiled fine.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Hi, Murali. Did you still have a question?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now