How to call private method of a class using Java in NetBeansIDE


Steps:


1. Open NetBeansIDE.

2. In your project make a class file.

3. In your class file make a class, say 'C' with a private method.

4. Make another class say 'Calling' and write the following code in it:

    public static void main(String[] args)throws Exception {
       
        Class c=C.class;
        Object obj=c.newInstance();
        Method m=c.getDeclaredMethod("msg", null);
        m.setAccessible(true);
        m.invoke(obj, null);
       
    }

5. Run the file.

6. Finish.

Thank You :)

Comments

Popular posts from this blog

How to Perform DES Encryption and Decryption of TXT file in JAVA with NetBeansIDE

How to Detect CapsLock key using Java in NetBeansIDE.

How to insert Image in MySQL Database with Java in NetBeansIDE