Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Ashty Ali
private method
1 Answer(s)      3 years and 11 months ago
Posted in : Java Beginners


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);
}
}

Advertisement
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
Private Cloud Hosting
Private Cloud Hosting  Hi, What is Private Cloud Hosting? What are the benefits of Private Cloud Hosting? Thanks
Java private constructor
Java private constructor  what is private constructor?what are the uses of writing private constructor in our program
private static final
private static final  Why there is necessary to make a variable private static final
Private Java Keyword
. It is applicable to a class, a method or a field.  private method...; Declaring Private Method... Private Java Keyword      
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
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
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
Virtual Private Servers
Virtual Private Servers A Virtual Private Server or VPS for short is also know as Virtual Dedicated Server (VDS). Virtual Private Servers is many servers... of a dedicated server to many customers as low cost. In case of Virtual Private Server
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
In Java, what's the difference between public, default, protected, and private?
In Java, what's the difference between public, default, protected, and private?  In Java, what's the difference between public, default, protected, and private
What's the proper way to test a class with private methods using JUnit?
What's the proper way to test a class with private methods using JUnit?  What's the proper way to test a class with private methods using JUnit
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
profile doesn't match any valid certificate private key pair in the default keychain
profile doesn't match any valid certificate private key pair in the default keychain   Hi, I am not able to create my application build. Following error is coming profile doesn't match any valid certificate private key pair
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
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 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
A method getColumnCount.
A method getColumnCount.  There is a method getColumnCount in the JDBC API. Is there a similar method to find the number of rows in a result set
abstract method
abstract method  is final method is in abstract class
method name
method name  Is there any method which is equivalent to c++'s delay(int) function
gc() method
gc() method  what is difference between java.lang.System class gc() method and java.lang.Runtime class gc() method
Static method
Static method  what is a static method?   Have a look at the following link: Java Static Method
abstract method
abstract method  Can a concrete class have an abstract method
 

 

 

DMCA.com