How to ivoke a method on an object of a private class

When I did it I got a java.lang.IllegalAccessException.

Well 'private' does have a well-known meaning in
Java, and it's been working in Java for at least 11
years. Surely you knew that?The combination "private class" seemed unusual
and made me de-focus from the rest of it.
Re your example, special rules apply to inner
classes. The access modifiers aren't tested by the
enclosing class.Yes. The only way I am aware of of having a "private class" (as the subject implies) is to have a nested class.
I agree one would be able to access methods on an instance of such a class only from the embedding class (or peer nested classes).

Similar Messages

  • How to reach a method of an object from within another class

    I am stuck in a situation with my program. The current situation is as follows:
    1- There is a class in which I draw images. This class is an extension of JPanel
    2- And there is a main class (the one that has main method) which is an extension of JFrame
    3- In the main class a create an instance(object) of StatusBar class and add it to the main class
    4- I also add to the main class an instance of image drawing class I mentioned in item 1 above.
    5- In the image drawing class i define mousemove method and as the mouse
    moves over the image, i want to display some info on the status bar
    6- How can do it?
    7- Thanks!

    It would make sense that the panel not be forced to understand its context (in this case a JFrame, but perhaps an applet or something else later on) but offer a means of tracking.
    class DrawingPanel extends JPanel {
      HashSet listeners = new HashSet();
      public void addDrawingListener(DrawingListener l) {
         listeners.add(l);
      protected void fireMouseMove(MouseEvent me) {
         Iterator i = listeners.iterator();
         while (i.hasNext()) {
            ((DrawingListener) i.next()).mouseMoved(me.getX(),me.getY());
    class Main implements DrawingListener {
      JFrame frame;
      JLabel status;
      DrawingPanel panel;
      private void init() {
         panel.addDrawingListener(this);
      public void mouseMoved(int x,int y) {
         status.setText("x : " + x + " y: " + y);
    public interface DrawingListener {
      void mouseMoved(int x,int y);
    }Of course you could always just have the Main class add a MouseMotionListener to the DrawingPanel, but if the DrawingPanel has a scale or gets embedded in a scroll pane and there is some virtual coordinate transformation (not using screen coordinates) then the Main class would have to know about how to do the transformation. This way, the DrawingPanel can encapsulate that knowledge and the Main class simply provides a means to listen to the DrawingPanel. By using a DrawingListener, you could add other methods as well (versus using only a MouseMotionListener).
    Obviously, lots of code is missing above. In general, its not a good idea to extend JFrame unless you really are changing the JFrames behavior by overrding methods, etc. Extending JPanel is okay, as you are presumably modifiying the drawing code, but you'd be better off extending JComponent.

  • How can I use methods of an object with private instantiation in my ABAP?

    Hello all,
    What is the work-around on using methods of an object marked as private instantiation? I tried to use concepts of <b>INHERITANCE</b> or <b>FRIENDS</b> of an object (<u>BCONTACT </u> in my case - a SAP standard object) but it did not work, or at least, I do not know how to properly code the statements in my ABAP program.
    Any code samples, other ideas?
    Your help is greatly appreciated.

    I am closing this question as there has been no answer.
    Message was edited by:
            Goharjou ardavan

  • JUNIT : how to call static methods through mock objects.

    Hi,
    I am writing unit test cases for an action class. The method which i want to test calls one static method of a helper class. Though I create mock of that helper class, but I am not able to call the static methods through the mock object as the methods are static. So the control of my test case goes to that static method and again that static method calls two or more different static methods. So by this I am testing the entire flow instead of testing the unit of code of the action class. So it can't be called as unit test ?
    Can any one suggest me that how can I call static methods through mock objects.

    The OP's problem is that the object under test calls a static method of a helper class, for which he wants to provide a mock class
    Hence, he must break the code under test to call the mock class instead of the regular helper class (because static methods are not polymorphic)
    that wouldn't have happened if this helper class had been coded to interfaces rather than static methods
    instead of :
    public class Helper() {
        public static void getSomeHelp();
    public class MockHelper() {
        public static void getSomeHelp();
    }do :
    public class ClassUnderTest {
        private Helper helper;
        public void methodUnderTest() {  // unchanged
            helper.getSomeHelp();
    public interface Helper {
        public void getSomeHelp();
    public class HelperImpl implements Helper {
        public void getSomeHelp() {
            // actual implementation
    public class MockHelper implements Helper {
        public void getSomeHelp() {
            // mock implementation
    }

  • How to execute a method from String object?

    I have some methods in String objects, example:
    String methodOne = "someMethod1";
    String methodTwo = "someMethod2";
    public void someMethod1() {
    public void someMethod2() {
    How can i execute that methods, when the method names i have only in String objects?

    You can get the method directly from the name and parameter types if you want to avoid using the loop:public class Foo27 {
      public static void main (String[] args) throws Exception{
        String methodOne = "someMethod1";
        String methodTwo = "someMethod2";
        Object foo = new Foo27();
        Method method1 = foo.getClass().getMethod(methodOne, new Class[]{});
        method1.invoke(foo, null);
        Method method2 = foo.getClass().getMethod(methodTwo, null);
        method2.invoke(foo, null);
      public void someMethod1() {
        System.out.println("someMethod1");
      public void someMethod2() {
        System.out.println("someMethod2");
    }The parameter types are required as there could be more than one method with the same name.
    For somereason when I tried to create a new Method object and invoke it, an exception was thrownThere isn't a public constructor for Method, so how did you create a new one?
    Pete

  • How to call paint() method during creating object

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    class SomeShape extends JPanel {
         protected static float width;
         protected BasicStroke line = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.CAP_ROUND);
    class Oval extends SomeShape {
         Oval(float width) {
              this.width = width;
              line = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.CAP_ROUND);
              repaint();
         public void paint(Graphics g) {
              Graphics2D pen = (Graphics2D)g;
              int i = 10;
              super.paint(g);
                   g.setColor(Color.blue);
                   g.drawOval(90, 0+i, 90, 90);
                   System.out.println("paint()");
    public class FinalVersionFactory {
        JFrame f = new JFrame();
        Container cp = f.getContentPane();
        float width = 0;
        SomeShape getShape() {
             return new Oval(width++); //I want to paint this oval when I call getShape() method
         public FinalVersionFactory() {
              f.setSize(400, 400);
    //          cp.add(new Oval()); without adding
              cp.addMouseListener(new MouseAdapter() {
                   public void mouseReleased(MouseEvent e) {
                        getShape();
              f.setVisible(true);
         public static void main(String[] args) { new FinalVersionFactory(); }
    }I need help. When I cliked on the JFrame nothing happened. I want to call paint() method and paint Oval when I create new Oval() object in getShape(). Can you correct my mistakes? I tried everything...Thank you.

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    class SomeShape extends JPanel {
         protected static float width;
         protected static BasicStroke line = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.CAP_ROUND);
    class Oval extends SomeShape {
         static int x, y;
         Oval(float width, int x, int y) {
              this.width = width;
              this.x = x;
              this.y = y;
              line = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.CAP_ROUND);
         public void paint(Graphics g) {
              Graphics2D pen = (Graphics2D)g;
                   g.setColor(Color.blue);
                   pen.setStroke(line);
                   g.drawOval(x, y, 90, 90);
                   System.out.println("Oval.paint()"+"x="+x+"y="+y);
    class Rect extends SomeShape {
         static int x, y;
         Rect(float width, int x, int y) {
              this.width = width;
              this.x = x;
              this.y = y;
              line = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.CAP_ROUND);
         public void paint(Graphics g) {
              Graphics2D pen = (Graphics2D)g;
                   g.setColor(new Color(250, 20, 200, 255));      
                   pen.setStroke(line);
                   g.drawRect(x, y, 80, 80);
                   System.out.println("Rect.paint()"+"x="+x+"y="+y);
    public class FinalVersionFactory extends JFrame {
        Container cp = getContentPane();
        float width = 0;
        int x = 0;
        int y = 0;
            boolean rect = false;
        SomeShape getShape() {
             SomeShape s;
              if(rect) {
                   s = new Rect(width, x, y);
                   System.out.println("boolean="+rect);
              } else {
                   s = new Oval(width++, x, y);
                   System.out.println("boolean="+rect);
              System.out.println("!!!"+s); //print Oval or Rect OK
              return s; //return Oval or Rect OK
         public FinalVersionFactory() {
              setSize(400, 400);
              SomeShape shape = getShape();
              cp.add(shape); //First object which is add to Container(Oval or Rect), returned by getShape() method
              //will be paint all the time. Why? Whats wrong?
              cp.addMouseListener(new MouseAdapter() {
                   public void mouseReleased(MouseEvent e) {
                        x = e.getX();
                        y = e.getY();
                        rect = !rect;
                        getShape();
                        cp.repaint(); //getShape() return Oval or Rect object
                                      //but repaint() woks only for object which was added(line 67) as first
              setVisible(true);
         public static void main(String[] args) { new FinalVersionFactory(); }
    }I almost finish my program but I have last problem. I explained it in comment. Please look at it and correct my mistakes. I will be very greatful!!!
    PS: Do you thing that this program is good example of adoption Factory Pattern?

  • How do I call methods with the same name from different classes

    I have a user class which needs to make calls to methods with the same name but to ojects of a different type.
    My User class will create an object of type MyDAO or of type RemoteDAO.
    The RemoteDAO class is simply a wrapper class around a MyDAO object type to allow a MyDAO object to be accessed remotely. Note the interface MyInterface which MyDAO must implement cannot throw RemoteExceptions.
    Problem is I have ended up with 2 identical User classes which only differ in the type of object they make calls to, method names and functionality are identical.
    Is there any way I can get around this problem?
    Thanks ... J
    My classes are defined as followes
    interface MyInterface{
         //Does not and CANNOT declare to throw any exceptions
        public String sayHello();
    class MyDAO implements MyInterface{
       public String sayHello(){
              return ("Hello from DAO");
    interface RemoteDAO extends java.rmi.Remote{
         public String sayHello() throws java.rmi.RemoteException;
    class RemoteDAOImpl extends UnicastRemoteObject implements RemoteDAO{
         MyDAO dao = new MyDAO();
        public String sayHello() throws java.rmi.RemoteException{
              return dao.sayHello();
    class User{
       //MyDAO dao = new MyDAO();
       //OR
       RemoteDAO dao = new RemoteDAO();
       public void callDAO(){
              try{
              System.out.println( dao.sayHello() );
              catch( Exception e ){
    }

    >
    That's only a good idea if the semantics of sayHello
    as defined in MyInterface suggest that a
    RemoteException could occur. If not, then you're
    designing the interface to suit the way the
    implementing classes will be written, which smells.
    :-)But in practice you can't make a call which can be handled either remotely or locally without, at some point, dealing with the RemoteException.
    Therefore either RemoteException must be part of the interface or (an this is probably more satisfactory) you don't use the remote interface directly, but MyInterface is implemented by a wrapper class which deals with the exception.

  • How to synchronize a method for all instances of a class

    Hi,
    How to make a method synchronized for all instances of a class? If a simple method is synchronized, then multiple threads cannot access it at the same time. If we make the method as static, then we are making it synchronized at class level.How to make a synchronized method so that no two instances (objects) of a class can access it at the same?
    Thanks
    Neha

    Neha_Khands wrote:
    There is nothing wrong with that. Actually this question was asked in an interview. They didnt want to create a static method. They told me that synchronization can be achieved at instance level also. and for that we have to call some Class.getInstance().synchronied method inside constructor. Kind of a dumb question. First, synchronization does not occur "at a class level" or "at an instance level." Syncing is always the same--a single object's lock is obtained, which prevents any other threads from obtaining that lock. The only thing that makes it appear that there are special cases is that declaring a method synchronized obtains the lock associated with the instance or with the Class object for that class. But that's just syntactic sugar. The Class object lock is identical to the instance lock, which in turn is identical to a lock on some other arbitrary Object created just to serve as a lock. There's no such things as "locking the class" or "locking the instance."
    Second, and more important, making an instance method synced across all instances is a grotesquely artificial situation, IMHO, and if it were to ever come up, the right way to do it is to have that instance method call a static synchronized method.

  • IN R12 -- HOW TO CREATE RECEIPT METHOD IN RECEIVABLES

    IN R12 --> HOW TO CREATE RECEIPT METHOD IN RECEIVABLES
    SET UP
    RECEIPTS-------> RECEIPT CLASSES
    I CREATED NEW RECEIPT CLASS AND NEW RECEIPT METHOD
    WHEN CREATING NEW RECEIPT METHOD
    BANK ACCOUNTS---------->
    I AM ABLE TO SELECT THE DETAILS FOR THE FOLLOWING
    1. OPERATING UNIT
    2. BANK NAME
    3. BRANCH NAME
    4. EFFECTIVE DATES
    5. CASH ACCOUNT
    6. UNAPPLIED RECEIPTS
    7. UNIDENTIFIED RECEIPTS
    8. ON ACCOUNT RECEIPTS
    BUT WHEN COMES TO THE
    UNEARNED DISCOUNTS AND
    EARNED DISCOUNTS I AM UNABLE TO GET LOV FOR THAT
    IN THE SET UP----> SYSTEM OPTIONS ----> MISCELLANEOUS
    I CHECKED THE CHECK BOX TO YES FOR
    ALLOW UNEARNED DISCOUNTS
    PLEASE GIVE SOLUTIONS FOR THIS
    THANKS IN ADVANCE
    PRINCE

    Hi
    Thank You Ketter Ohnes for Reply,
    AR: Setup: Receipts: Receivable Activities
    When i am doing above set up it is giving the following error.
    PLEASE DEFINE A PARTY TAX PROFILE FOR OPERATING UNIT & ORG_ID
    Thanks and Regards
    Prince

  • Accessing objects in a different class?

    I am now trying to reference a JFileChooser that is in my properties class to a String in my ConfCode class. How would I go about referencing this Object outside of its class? If this makes sense!

    If both classes are located in the same directory, you should be able to call them just like any other class you've imported.
    //source file Class1.java
    public class Class1 {
        public static void main(String[] args) {
         Class2 c2 = new Class2();
         c2.theMethod();
    //source file Class2.java
    public class Class2 {
        public void theMethod() {
         System.out.println("the method was called!");
    }

  • How to use the index method for pathpoints object in illustrator through javascripts

    hii...
    am using Illustrator CS2 using javascripts...
    how to use the index method for pathpoints object in illustrator through javascripts..

    Hi, what are you trying to do with path points?
    CarlosCanto

  • How to invoke a method in application module or view objects interface?

    Hi,
    perhaps a stupid RTFM question, but
    How can i invoke a method that i have published in the application modules or view objects interfaces using uiXml with BC4J?
    Only found something how to invoke a static method (<ctrl:method class="..." method="..." />) but not how to call a method with or without parameters in AM or VO directly with a uix-element.
    Thanks in advance, Markus

    Thanks for your help Andy, but i do not understand why this has to be that complicated.
    Why shall i write a eventhandler for a simple call of a AM or VO method? That splatters the functionality over 3 files (BC4J, UIX, handler). Feature Request ;-)?
    I found a simple solution using reflection that can be used at least for parameterless methods:
    <event name="anEvent">
      <bc4j:findRootAppModule name="MyAppModule">
         <!-- Call MyAppModule.myMethod() procedure. -->
          <bc4j:setPageProperty name="MethodName" value="myMethod"/>
          <ctrl:method class="UixHelper"
                  method="invokeApplicationModuleMethod"/>
       </bc4j:findRootAppModule>
    </event>The UixHelper method:
      public static EventResult invokeApplicationModuleMethod( BajaContext context,
                                                               Page page,
                                                               PageEvent event ) {
        String methodName = page.getProperty( "MethodName" );
        ApplicationModule am = ServletBindingUtils.getApplicationModule( context );
        Method method = null;
        try {
          method = am.getClass(  ).getDeclaredMethod( methodName, null );
        } catch( NoSuchMethodException e ) {
          RuntimeException re = new RuntimeException( e );
          throw re;
        try {
          method.invoke( am, null );
        } catch( InvocationTargetException e ) {
          RuntimeException re = new RuntimeException( e );
          throw re;
        } catch( IllegalAccessException e ) {
          RuntimeException re = new RuntimeException( e );
          throw re;
        return null;
      }Need to think about how to handle parameters and return values.
    Btw. Do i need to implement the EventHandler methods synchronized?
    Regards, Markus

  • How to call the method of a Business Object?

    Hi,
    Can someone guide me how to call the method of a business object?
    For example, I want to use the method SalesDocument.Copy of the Business Object VBAK. How can I do that? If you are familiar with any similar scenario please help.
    Regards,
    Renjith Michael.

    Hi
    double click on the copy  and
    go to abap tab
    there u can get functionmodule name
    u can call that
    Rewards if helpful

  • How do interfaces have methods of Object class

    Hi All,
    Please consider the following code snippet.
    public interface EmployeeService
        public void createEmployee(Employee emp);
        public Employee findEmployee(String empId);
    public class EmployeeServiceImpl implements EmployeeService
        public void createEmployee(Employee emp) { ................. }
        public Employee findEmployee(String empId) { ................. }
    }The above is a simple example where I have an employee object with two service methods to create and find an employee.
    Now the consider the following
    EmployeeService empService = new EmployeeServiceImpl();
    Employee emp = empService.findEmployee("1").So the above code helps me in finding an employee.
    Now, we know that you can only call those methods that are defined in the interface.
    My question is, if I use the empService you would be able to access the methods of the Object class (equals, hashCode, wait etc.)
    How does this happen? In the Jave API we know all class by default override the Object class, so how does it work with interfaces?
    Thanks in advance for the reply

    [JLS 6.4.4 The Members of an Interface Type|http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.4.4] says:
    If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.
    So, in short, those methods exist in interfaces because the JLS says they do.

  • How to call Apps Module Custom Method from Entity Object / View Object ?

    Hi All,
    I create a custom method in AppsModuleImpl.java. How can I call that custom method from a setter method on Entity Object / View Object ?
    (I have tried to use Configuration.createRootApplicationModule(amDef,config); but it is not the correct way, is it? )
    Below is my code :
    The setter on MyEntityImpl.java :
    public void setKodeprd(String value) {
    setAttributeInternal(KODEPRD, value);
    if (getProduct() != null) {
    // CALL the getSalesPrice custom method from here, HOW ??
    The Application Module custom method :
    public Number getSalesPrice(String priceCode, String kodePrd) {
    Number price = new Number(0);
    String [] theKey = {priceCode, kodePrd};
    Key priceKey = new Key(theKey) ;
    ViewObject SalesPrice = getSalespricedView1();
    Row[] salesPriceRow = SalesPrice.findByKey(priceKey, 1);
    price = ((SalespricedViewRowImpl)salesPriceRow[0]).getPrice();
    SalesPrice.remove();
    return price;
    Thank you for your help,
    xtanto

    inside the EntityObjectImpl :
    YourAppModuleImpl am = (YourAppModuleImpl)getDBTransaction().getRootApplicationModule().findApplicationModule("YourAppModuleorServiceName");

Maybe you are looking for

  • Firefox new version 4 wouldn't load, I have MAC OS X 10.5.8 how do I get my old version of Firefox version 3 back on my laptop?

    I have MAC OS X 10.5.8 laptop. I had Firefox version 3 on and got email to upgrade to version 4. I downloaded it and couldn't get it into applications. Symbol had circle w/ line thru it. Pop up screen said it is not supported on this architecture. I

  • How need i configure a servlet in NES 4.1 TO PROXI REQUEST TO WEBLOGIC

    I have some problems to configure NSAPI plug-in to work correctly, do u now if is necesary configure another file that obj.conf and have the correct version of libproxy.so.Have some clues for my?I was reading the information available in www.weblogic

  • In the report ME2N

    Hi, In the report Me2n, I have come across a new requirement to list Open PO's with Movement type 124 (124 Return delivery from Blocked GR stock). Can we add this to the "Selection Parameter" and be able to report. With Regards, jaheer

  • Some updates failed to install laptop on OSX 10.5.8

    attempted to download updates from the "help" section and got a screen that reads Adobe Bridge CS5 4.0.5 Update   There was an error downloading this update. Please quit and try again later.   Photoshop 12.0.4 update for Photoshop CS5   There was an

  • Inherited display method - Reply

    If a method that is defined in a super class, is ALSO defined in a child class, that child method is not automatically filled with the code in the parent's method of the same name. This is 'overriding' a method. Any common code would need to be in th