Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:49:52+00:00 2026-05-30T14:49:52+00:00

I am trying to invoke a private method belonging to one class from another

  • 0

I am trying to invoke a private method belonging to one class from another class using java reflection. Both these classes belong to different packages. Code sample is as below. But everytime I run the getDeclaredMethod it returns with NoSuchMethodException. How do I invoke the getCacheKey method from my class?

Thanks,

Class A

package com.abc;

public class TicketHelper
{
    static String getCacheKey(String ticketString, Ticket ticket) throws TicketException, UnsupportedEncodingException, NoSuchAlgorithmException {
      ...
    }
}

Class B

package com.def;

...

private Method method = null;

public class TicketHelper
{
    ...

    try {
        method = TicketHelper.class.getDeclaredMethod("getCacheKey", new Class[] {String.class, Ticket.class});
        } catch (SecurityException e1) {
            setTrace("Security exception2 " + e1.getMessage());
        } catch (NoSuchMethodException e1) {
            setTrace("No such method exception2 " + e1.getMessage());
    }
    method.setAccessible(true);
    m_cacheKey = method.invoke(null, new Object[] {ticketString, ticket});
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:49:53+00:00Added an answer on May 30, 2026 at 2:49 pm

    Is the class in com.def also called TicketHelper? In that case you need to qualify as com.abc.TicketHelper

    EDIT

    There are several compilation errors in the code you posted. Always try to come up with a short example that reproduces the problem; in most cases you will see you error in that process. The following works for me. It is same package, but that should not matter:

    public class TicketHelperUser
    {
        public static void main(String[] args) throws Exception
        {
        for (java.lang.reflect.Method m : TicketHelper.class.getDeclaredMethods())
        {
            System.out.println(m);
        }
        java.lang.reflect.Method method = TicketHelper.class.getDeclaredMethod("getCacheKey", String.class, Ticket.class);
        method.setAccessible(true);
        method.invoke(null, new Object[] {"", new Ticket()});
        }
    }
    
    public class TicketHelper
    {
        static String getCacheKey(String ticketString, Ticket ticket) 
        {
        return "cacheKey";
        }
    
    }
    
    public class Ticket {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to invoke a method that takes a super class as a parameter
I'm currently trying to invoke a method made in C from C# C code
I am trying to invoke the CreateFile method from Kernel32.dll in a C# application,
Am trying to invoke a public web-service using a java client but am getting
I got this DomainService method I'm calling from my SL ViewModel using the Invoke
When trying to invoke a method on an external webservice (over the Internet) it
I am trying to invoke an ANT target from Windows (right-click) file context menu.
I'm currently trying to invoke a web service from a web application that I've
I'm trying to write a custom method security interceptor. However, it isn't using the
I'm trying to unit-test some classes that make use of a Singleton class whose

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.