Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

public and private method

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN
How can i access a private method of a class ? whats the syntax ?

Code:
class A
{

int x;

private void method()
{
// do something
}


}
How can i access this method in another class B ?
 
You can't - thats the point of a method being private - only the class that defines the method can access it.

--------------------------------------------------
Free Database Connection Pooling Software
 
Well, you actually can, but you shouldn't.

What are you trying to do?


Cheers,

Dian
 
You can do it by using reflection. It's one of the worst practice I've ever seen, but I've used it myself on emergency situations.

It's something like:

Code:
Class clazz = methodClass.getClass();
Method privateMethod = clazz.getDeclaredMethod("privateMethod");
privateMethod.setAccesible(true);
privateMethod.invoke(blah, blah ....)

Cheers,

Dian
 
Code:
public class Test
{
private void method()
{
System.out.println("Hello");	
}	
}

class Hello 
{
public static void main(String[] args)
{Test t= new Test(); // i'm creating an instance of class Test
t.method(); // t is using its own method. i got compile error
}
}

As t is using its own method. so it is supposed to work . but it is not working. throwing compile error "method() has private access in Test.

it is a mystery, that even its own instance are prohibited in accessing the method!
why ? so is this private keyword a useless keyword ? can you tell an example where i could use it ?
 
when do i use this "private" keyword ? any example ?
 
Humm.

When you do someClass.someMethod() you're accessing that method from outside the class, so you will just hace access to its public methods.

To access the private methods, you must be inside the same instance of the class, inside a method of that instance of that class.

I'm not sure if that makes sense for you.

You can close the window of a car just form inside that car, not from inside of any car.

HTH,

Dian
 
Well, Diancecht, that's not absolute correct.
You may call a private method from inside the class, and the point here is, from inside the code, representing the class.

The instance is of no interest here, and it was surprising for me too, when I heard it the first time.

Example

Code:
class Demo
{
   private int bar;
   
   public Demo (int b)
   {
      bar = b;
   }

   private foo ()
   {
       bar *= 2;     
   }

   public foobar (Demo d)
   {
       if (d.bar < bar) d.foo();
   }
}

seeking a job as java-programmer in Berlin:
 
@satelitte03:

The private syntax is of multiple usage.

If a piece of code is needed multiple time in a class, you may make a private method from it. This will make your coding faster, bugfixing more easy, reading it faster. You may follow the rule 'oaoo' - Once, and only once.

If a method is growing big, you may split it into small logical entities. This will make your code more readable, testable, maintainable. Don't exceed one screenpage.

Think of an operation, which will succeed under certain preconditions, and fail with others.
And sometimes your class might guarantee these conditions, and sometimes not.
So you may put the operation into an private method, and call it directly, when the precondition matches, and check the condition only where uncertain.



seeking a job as java-programmer in Berlin:
 
You can close the window of a car just form inside that car, not from inside of any car
he he good comment.


normally, we make many classes. and we wish to use these classes in other classes. so we should make the methods of classes public so that those methods would be available in other classes. but if we do not declare methods to be public then nobody can use that. then whats the use of those methods?

what did you say ? , you can use it for bugfixing, reading faster. well, ok , but that too can also be done if i usued public method. it means there is no absolute importance(except bugfixing, reading faster) of these private methods while writing a class. and i can forget about this private method because,its never usued, you can bypass it.
 
I generally go by the rule that if a method is public, then it should generally be able to "run as is", perhaps after some initial construction.

A method should be private if you either don't want anyone to use it, or there is so much reliance on other methods, or class wide variables that the user of that method may get unexpected results.

Anything that you do over and over again should be put into a "util" or "common" package where all code can access the particular methods.

You can also have protected methods which can be accessed by local package classes - but lets not go there !

--------------------------------------------------
Free Database Connection Pooling Software
 
It means there is no absolute importance(except bugfixing, reading faster) of these private methods while writing a class. and i can forget about this private method because,its never usued, you can bypass it.

No. You can only bypass it from inside - if you have access to the source code.
Or by reflection without source-code.

But it avoids unintended usage by accident.

Of course you may code in assembler.

seeking a job as java-programmer in Berlin:
 
satellite03, the whole point of private methods is to allow you to write well-structured code within your class and hide the bits which are specific to the implementation of the class from outside influence. They allow you to break your code down into discrete methods, some of which will be exposed to other classes to satisfy the responsibilities of that class, whilst others (the private ones) deal with the inner-workings of the class and should do it 'behind closed doors'.

They are an essential part of OO programming; public methods determine what a class can do for others, whilst private methods are part of how it gets the jobs done. The idea is that if no outside class can 'get at' the how bits, then we are free (within reason) to take a class and completely overhaul how it does its jobs without affecting the rest of the system. If they were made public and other classes made direct use of them, then that class's inner workings could never change without that change propagating to all those external classes.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top