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









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
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
Private Java Keyword
. It is applicable to a class, a method or a field.  private method... Declaring Private Method public... Private Java Keyword      
Java private constructor
Java private constructor  what is private constructor?what are the uses of writing private constructor in our program
Private Cloud Hosting
Private Cloud Hosting  Hi, What is Private Cloud Hosting? What are the benefits of Private Cloud Hosting? Thanks
private and final - Java Interview Questions
private and final  what is the difference between private modifier and final modifier in java   Hi Friend, Private modifier indicates that the variable\method\class can only be accessed within the class and cannot
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 static final
private static final  Why there is necessary to make a variable private static final
Private variable of class
Private variable of class  Hi..... What keyword allows you to refer to private variables of a class in flex? Please give me an example for that Thanks  Ans: private keyword and this keyword
Maven Repository/Dependency: s3-wagon-private | s3-wagon-private
Maven Repository/Dependency of Group ID s3-wagon-private and Artifact ID s3-wagon-private. Latest version of s3-wagon-private:s3-wagon-private dependencies. # Version Release Date You can
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 '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 '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 '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
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"
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
What do you understand by private, protected and public?
What do you understand by private, protected and public?   Hi, What do you understand by private, protected and public? thanks
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
Private vs Public Blockchain
Private vs Public Blockchain - Similarities and differences between two types... the Private and Public. These two types of Blockchain chains are used to develop applications for public and private network. Developer should learn
Private vs Public Blockchain
Private vs Public Blockchain - Similarities and differences between two types... the Private and Public. These two types of Blockchain chains are used to develop applications for public and private network. Developer should learn
method question
method question  How do I figure out the difference in a method heading, a method body, and a method definition
ModuleNotFoundError: No module named 'django-private-chat'
ModuleNotFoundError: No module named 'django-private-chat'  Hi, My... named 'django-private-chat' How to remove the ModuleNotFoundError: No module named 'django-private-chat' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-private-files'
ModuleNotFoundError: No module named 'django-private-files'  Hi...: No module named 'django-private-files' How to remove the ModuleNotFoundError: No module named 'django-private-files' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-private-files-0.1.2'
ModuleNotFoundError: No module named 'django-private-files-0.1.2'  Hi...: No module named 'django-private-files-0.1.2' How to remove the ModuleNotFoundError: No module named 'django-private-files-0.1.2' error? Thanks
ModuleNotFoundError: No module named 'django-private-media'
ModuleNotFoundError: No module named 'django-private-media'  Hi...: No module named 'django-private-media' How to remove the ModuleNotFoundError: No module named 'django-private-media' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-private-messages'
ModuleNotFoundError: No module named 'django-private-messages'  Hi...: No module named 'django-private-messages' How to remove the ModuleNotFoundError: No module named 'django-private-messages' error? Thanks   
ModuleNotFoundError: No module named 'django-versatileimagefield-private'
ModuleNotFoundError: No module named 'django-versatileimagefield-private' ...: ModuleNotFoundError: No module named 'django-versatileimagefield-private' How to remove the ModuleNotFoundError: No module named 'django-versatileimagefield-private
ModuleNotFoundError: No module named 'private-pypi-core'
ModuleNotFoundError: No module named 'private-pypi-core'  Hi, My... named 'private-pypi-core' How to remove the ModuleNotFoundError: No module named 'private-pypi-core' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'private-pypi-github'
ModuleNotFoundError: No module named 'private-pypi-github'  Hi, My... named 'private-pypi-github' How to remove the ModuleNotFoundError: No module named 'private-pypi-github' error? Thanks   Hi
ModuleNotFoundError: No module named 'pybbm-private-messages'
ModuleNotFoundError: No module named 'pybbm-private-messages'  Hi...: No module named 'pybbm-private-messages' How to remove the ModuleNotFoundError: No module named 'pybbm-private-messages' error? Thanks   Hi
ModuleNotFoundError: No module named 'steven-private-package-for-python'
ModuleNotFoundError: No module named 'steven-private-package-for-python' ...: ModuleNotFoundError: No module named 'steven-private-package-for-python' How to remove the ModuleNotFoundError: No module named 'steven-private-package-for-python'
ModuleNotFoundError: No module named 'django-private-chat'
ModuleNotFoundError: No module named 'django-private-chat'  Hi, My... named 'django-private-chat' How to remove the ModuleNotFoundError: No module named 'django-private-chat' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-private-files'
ModuleNotFoundError: No module named 'django-private-files'  Hi...: No module named 'django-private-files' How to remove the ModuleNotFoundError: No module named 'django-private-files' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-private-files-0.1.2'
ModuleNotFoundError: No module named 'django-private-files-0.1.2'  Hi...: No module named 'django-private-files-0.1.2' How to remove the ModuleNotFoundError: No module named 'django-private-files-0.1.2' error? Thanks
ModuleNotFoundError: No module named 'django-private-media'
ModuleNotFoundError: No module named 'django-private-media'  Hi...: No module named 'django-private-media' How to remove the ModuleNotFoundError: No module named 'django-private-media' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-private-messages'
ModuleNotFoundError: No module named 'django-private-messages'  Hi...: No module named 'django-private-messages' How to remove the ModuleNotFoundError: No module named 'django-private-messages' error? Thanks   
ModuleNotFoundError: No module named 'django-private-storage'
ModuleNotFoundError: No module named 'django-private-storage'  Hi...: No module named 'django-private-storage' How to remove the ModuleNotFoundError: No module named 'django-private-storage' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-private-views'
ModuleNotFoundError: No module named 'django-private-views'  Hi...: No module named 'django-private-views' How to remove the ModuleNotFoundError: No module named 'django-private-views' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-versatileimagefield-private'
ModuleNotFoundError: No module named 'django-versatileimagefield-private' ...: ModuleNotFoundError: No module named 'django-versatileimagefield-private' How to remove the ModuleNotFoundError: No module named 'django-versatileimagefield-private
ModuleNotFoundError: No module named 'flaskbb-plugin-private-memberlist'
ModuleNotFoundError: No module named 'flaskbb-plugin-private-memberlist' ...: ModuleNotFoundError: No module named 'flaskbb-plugin-private-memberlist' How to remove the ModuleNotFoundError: No module named 'flaskbb-plugin-private-memberlist'
ModuleNotFoundError: No module named 'instagram-private-api'
ModuleNotFoundError: No module named 'instagram-private-api'  Hi...: No module named 'instagram-private-api' How to remove the ModuleNotFoundError: No module named 'instagram-private-api' error? Thanks   Hi
ModuleNotFoundError: No module named 'instagram-private-api-extensions'
ModuleNotFoundError: No module named 'instagram-private-api-extensions' ...: ModuleNotFoundError: No module named 'instagram-private-api-extensions' How to remove the ModuleNotFoundError: No module named 'instagram-private-api-extensions'

Ads