?
Solved

Calling private methods in main method

Posted on 2005-02-25
13
Medium Priority
?
252 Views
Last Modified: 2013-11-23
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
Comment
Question by:bmuralimohan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 750 total points
ID: 13408994
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13409051
Ignore my previous comment. I was thinking in terms of static
Sorry for wrong answering

0
 
LVL 92

Expert Comment

by:objects
ID: 13409088
> it used to be the same case even in older versions like Java1.1.6 etc.

:)
0
Industry Leaders: 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!

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13409102
Mick,

I just a get the doubt like what Murali got.

Howcome instance.private variables can be accessed??
0
 
LVL 92

Expert Comment

by:objects
ID: 13409134
> Howcome instance.private variables can be accessed??

Cause its being accessed from a method of Test
0
 
LVL 15

Expert Comment

by:JakobA
ID: 13409137
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13409141
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13409148
Seeing this difference in above class, I need to refresh my oops concepts once again :-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13409773
:)
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 13411597
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
 
LVL 92

Expert Comment

by:objects
ID: 13412221
I tested the above code on 1.1.8 and it compiled fine.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13413509
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
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 13455926
Hi, Murali. Did you still have a question?
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question