private method

private method

how can define private method in a class and using it in another class in a same package? my source code:

class rectangle{
int length,breadth;
void show(int length,int bredth){
this.length=length;
this.breadth=breadth;
}
public int calculate(){
int size;
size=length*breadth;
return(size);
}
}

public class useOfthisoperator  {
public static void main(String args[]){
    rectangle r=new rectangle();
    r.show(5,6);
    int area=r.calculate();
    System.out.println("this Area of rectangle is"+area);
}
}
View Answers

January 10, 2011 at 12:55 PM

Hi Friend,

Try the following code:

import java.lang.reflect.*;
class rectangle{
    int length,breadth;
    void show(int l,int b){
        this.length=l;
        this.breadth=b; 
        }
        private int calculate(){
            int size;
            size=length*breadth;
            return size;
        }
    }
public class useOfthisoperator {
    public static void main(String args[])throws Exception{ 
        rectangle r=new rectangle();
        r.show(5,6); 
        Method meth = rectangle.class.getDeclaredMethod("calculate");
        meth.setAccessible(true);
        Integer result = (Integer)meth.invoke(r);

System.out.println("Area of rectangle= " + result);
    }
}

Thanks

Ads









Related Tutorials/Questions & Answers:
private method
private method  how can define private method in a class and using...){ this.length=l; this.breadth=b; } private int calculate...(5,6); Method meth = rectangle.class.getDeclaredMethod("calculate
private method
private method  how can define private method in a class and using it in another class in a same package? my source code: class rectangle{ int length,breadth; void show(int length,int bredth){ this.length=length
Advertisements
What if the main() method is declared as private?
What if the main() method is declared as private?  Hi, What if the main() method is declared as private? Thanks   Hi, I have found some reference site for java programming coding for Main() method is declared
private and protected
private and protected  what is diffrence between private and protected
private constructor
private constructor  can we extend from a class which is having private constuctor
method
method   how and where, we can define methods ? can u explain me with full programme and using comments
Private Java Keyword
. It is applicable to a class, a method or a field.  private method...; Declaring Private Method... Private Java Keyword      
method
method  can you tell me how to write an abstract method called ucapan() for B2 class class A2{ void hello(){ system.out.println("hello from A2"); }} class B2 extends A2{ void hello(){ system.out.println("hello from B2
method inside the method??
method inside the method??  can't we declare a method inside a method in java?? for eg: public class One { public static void main(String[] args) { One obj=new One(); One.add(); private static void add
Private Cloud Hosting
Private Cloud Hosting  Hi, What is Private Cloud Hosting? What are the benefits of Private Cloud Hosting? Thanks
private static final
private static final  Why there is necessary to make a variable private static final
udacity secure and private ai
udacity secure and private ai  Hi, I am beginner in Data Science... secure and private ai Try to provide me good examples or tutorials links so that I can learn the topic "udacity secure and private ai". Also tell me
ModuleNotFoundError: No module named 'base-private'
ModuleNotFoundError: No module named 'base-private'  Hi, My Python... 'base-private' How to remove the ModuleNotFoundError: No module named 'base-private' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'kjkiss-private'
ModuleNotFoundError: No module named 'kjkiss-private'  Hi, My... named 'kjkiss-private' How to remove the ModuleNotFoundError: No module named 'kjkiss-private' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'mediagoblin-private'
ModuleNotFoundError: No module named 'mediagoblin-private'  Hi, My... named 'mediagoblin-private' How to remove the ModuleNotFoundError: No module named 'mediagoblin-private' error? Thanks   Hi
ModuleNotFoundError: No module named 'private-pypi'
ModuleNotFoundError: No module named 'private-pypi'  Hi, My Python... 'private-pypi' How to remove the ModuleNotFoundError: No module named 'private-pypi' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'PyTestGame_private'
ModuleNotFoundError: No module named 'PyTestGame_private'  Hi, My... named 'PyTestGame_private' How to remove the ModuleNotFoundError: No module named 'PyTestGame_private' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'mediagoblin-private'
ModuleNotFoundError: No module named 'mediagoblin-private'  Hi, My... named 'mediagoblin-private' How to remove the ModuleNotFoundError: No module named 'mediagoblin-private' error? Thanks   Hi
get method
get method   how to use get method: secure method is post method and most of use post method why use a get method
method question
method question  How do I figure out the difference in a method heading, a method body, and a method definition
Method
Method       In this section, we will explore the concept of method in the reference of object oriented... and behaviors are defined by methods. Method : An brief introduction Methods
explain this method
explain this method   i hope any one can explain this method ` private void showMonth(int m, int y) { int lead_spaces = 0; if (m...(); } } private static void doSimpleDateFormat() { Calendar now
Method Overloading
Method Overloading  In java can method be overloaded in different class
Method Overloading
Method Overloading  In java can method be overloaded in different class
private equity data science jobs
private equity data science jobs  Hi, I am beginner in Data Science...: private equity data science jobs Try to provide me good examples or tutorials links so that I can learn the topic "private equity data science jobs"
abstract method
abstract method  is final method is in abstract class
Static method
Static method  what is a static method?   Have a look at the following link: Java Static Method
gc() method
gc() method  what is difference between java.lang.System class gc() method and java.lang.Runtime class gc() method
method name
method name  Is there any method which is equivalent to c++'s delay(int) function
Checkbox method
Checkbox method  what is the method to check if the checkbox is checked
main method
main method  Why is the java main method static

Ads