?
Solved

Java: How do I call a public member function from within the class that it resides?

Posted on 2005-04-17
5
Medium Priority
?
258 Views
Last Modified: 2010-04-17
I have a class

class Foo()
         
        // private
             // code....
             // code...


        // default constructor
        public Foo()
        {
                 Function( );                           //<---------------------------------- I want to do this but I dont know how to make the class refer to itself.
                 // code...                               //                                                because this should be something of the form  .Function();
                 // code...                              //                                                 In C++ all I do is call the funciton, how do I in java?

        }

        public Function( )
        {
                // code..
                // code....
        }
0
Comment
Question by:travishaberman
  • 3
5 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 13802271
Everything there is fine, except for this:

        public Function( )
        {
                // code..
                // code....
        }

You have declared a method with no return type; only constructors can have no return type. Replace that with this:

        public void Function( )
        {
                // code..
                // code....
        }


That will work then.. However, if for some reason, you want to make it clear that you're using a local method, you can call a method with the use of the 'this' keyword, which represents the current, local instance, for example:

   this.Function();

would call the function 'Fuction()' in the current instance.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13802276
If you want to create a sub-class however, you can do something like this:


class Foo
{
   
   public Foo()
   {
      // code
      Function f = new Function();
   }
   
   class Function
   {
      public Function()
      {
         // Code
      }
   }
}


Regards;
0
 
LVL 1

Expert Comment

by:Tag_Markman
ID: 13803140

What you are doing should work. Here is a quick helloworld type program which uses a Method to initialize components for a JFrame: (normally initComponents() would be private not public but i'm showing that you can do it from a public as well.


public class HelloJFrame extends javax.swing.JFrame {
   
    private javax.swing.JLabel jLabel1;
   
// default constructor
    public HelloJFrame() {
        initComponents();
    }
   
// method
    public void initComponents() {
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("Howdy");
        getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);

        pack();
    }
       
// main
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HelloJFrame().setVisible(true);
            }
        });
    }
}


0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13804194
Hi,

Basically, your public Function() is the public function of your Foo class so the above codes should run. To make you more clear, let me give you same sample.

public class Foo()
{
    private String id;

    public void setID(String temp)
    {
       this.id = temp;
    }

    public String getID()
    {
        return this.id;
    }

    public String getMyIDPlease()
    {
        return getID();  // Call getID from getMyIDPlease()
    }

    public void pleaseStoreThisToMyID(String newID)
    {
        setID(newID);  // Call setID from pleaseStoreThisToMyID(String temp)
    }
}

So you can see that within the same class, you can actually call the functions. You can call it using System.out.println("My ID is : " + getMyIDPlease()); and pleaseStoreThisToMyID("2");

I hope that helps.

Best Regards
Dave
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13905232
:-)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Progress
Starting up a Project

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question