473,396 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

non-static method problem: how to access non-static method from another .java file?

17
i now learning Object Oriented Programming but stuck. Need help. Below is the code.


Expand|Select|Wrap|Line Numbers
  1.  PanelOption.java
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6.  
  7. public class PanelOption extends JPanel
  8. {
  9.     //some code
  10.  
  11.     public PanelOption()
  12.     {
  13.         createOptionGUI();
  14.     }
  15.  
  16.     public void createOptionGUI()
  17.     {                        
  18.         //some code
  19.     }
  20.  
  21.     public class ButtonListener implements ActionListener
  22.     {
  23.         public void actionPerformed(ActionEvent e) 
  24.         {            
  25.             if (e.getSource()==jbtnEnter)
  26.             {
  27.                 PanelQuestion.Question01(); // <= i wan to access to this void method but fail! How to solve?
  28.  
  29.             } 
  30.         }    
  31.     }
  32. }
  33.  
  34. //======================================================================    
  35.  
  36. PanelQuestion.java
  37. import java.awt.*;
  38. import java.awt.event.*;
  39. import javax.swing.*;
  40. import javax.swing.event.*;
  41.  
  42. public class PanelQuestion extends JPanel
  43. {
  44.     //some code
  45.  
  46.     public PanelQuestion()
  47.     {
  48.         createQuestionGUI();
  49.     }
  50.  
  51.     public void createQuestionGUI()
  52.     {
  53.         //some code
  54.     }
  55.  
  56.     public void Question01()
  57.     {
  58.         //some code  (this is the void method that i want to invoke by PanelOption.java)
  59.     }
  60. }
Jun 14 '07 #1
16 2440
epots9
1,351 Expert 1GB
i now learning Object Oriented Programming but stuck. Need help. Below is the code.


PanelOption.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PanelOption extends JPanel
{
//some code

public PanelOption()
{
createOptionGUI();
}

public void createOptionGUI()
{
//some code
}

public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==jbtnEnter)
{
PanelQuestion.Question01(); // <= i wan to access to this void method but fail! How to solve?

}
}
}
}

//================================================== ====================

PanelQuestion.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PanelQuestion extends JPanel
{
//some code

public PanelQuestion()
{
createQuestionGUI();
}

public void createQuestionGUI()
{
//some code
}

public void Question01()
{
//some code (this is the void method that i want to invoke by PanelOption.java)
}
}
Hi xirowei, welcome to TSDN,

create an object of type PanelQuestion
Expand|Select|Wrap|Line Numbers
  1. PanelQuestion pq = new PanelQuestion();
  2.  
then call the the method
Expand|Select|Wrap|Line Numbers
  1. pq.Question01();
  2.  
all non-static methods must be perfixed with an object.

good luck
Jun 14 '07 #2
JosAH
11,448 Expert 8TB
Consider the following scenario: my name is Jos, I'm an instantiation of the
class SillyPerson. You can tell me things:

Expand|Select|Wrap|Line Numbers
  1. public void tell(String message) {
  2. ...
  3. }
  4.  
If you want to tell me something you have to do this:

Expand|Select|Wrap|Line Numbers
  1. SillyPerson jos= new SillyPerson();
  2. jos.tell("you are silly");
  3.  
and the message would be sent to me.

You can't just do this:

Expand|Select|Wrap|Line Numbers
  1. SillyPerson.tell("you are silly");
  2.  
because you are talking to an instance of the class SillyPerson (e.g. me) not
to the class, the blueprint of all silly persons. In other words: you need at least
an instantiation to talk to.

kind regards,

Jos
Jun 14 '07 #3
r035198x
13,262 8TB
Consider the following scenario: my name is Jos, I'm an instantiation of the
class SillyPerson. You can tell me things:

Expand|Select|Wrap|Line Numbers
  1. public void tell(String message) {
  2. ...
  3. }
  4.  
If you want to tell me something you have to do this:

Expand|Select|Wrap|Line Numbers
  1. SillyPerson jos= new SillyPerson();
  2. jos.tell("you are silly");
  3.  
and the message would be sent to me.

You can't just do this:

Expand|Select|Wrap|Line Numbers
  1. SillyPerson.tell("you are silly");
  2.  
because you are talking to an instance of the class SillyPerson (e.g. me) not
to the class, the blueprint of all silly persons. In other words: you need at least
an instantiation to talk to.

kind regards,

Jos
Indeed welcome to TSDN. As you can see we have all sorts of people here. Now you see how your code looks under code tags? So next time you post code just wrap it inside code=java tags. the tags go inside square brackets []
Jun 14 '07 #4
JosAH
11,448 Expert 8TB
As you can see we have all sorts of people here.
No we don't:

Expand|Select|Wrap|Line Numbers
  1. Collection<SillyPerson> TSDN= new ArrayList<SillyPerson>();
  2.  
kind regards,

Jos ;-)
Jun 14 '07 #5
xirowei
17
epots9 thank you very much. You guide is really simple and easy to understand. Now it solve my problem of the non-static problem.
Jun 14 '07 #6
r035198x
13,262 8TB
epots9 thank you very much. You guide is really simple and easy to understand. Now it solve my problem of the non-static problem.
Well the SillyPerson example really looked good too. It told quite a lot of true things which you would have done good to notice.
Jun 14 '07 #7
epots9
1,351 Expert 1GB
i won't disagree with u there r035198x, but still

Jun 14 '07 #8
JosAH
11,448 Expert 8TB
:-)

kind regards,

Jos
Jun 14 '07 #9
r035198x
13,262 8TB
:-)

kind regards,

Jos
Expand|Select|Wrap|Line Numbers
  1. TSDNAdmin nonSillyPerson = TSDN.getAdmin();
  2. for(SillyPerson silly : TSDN) {
  3.      if(silly instanceOf Jos || silly instanceOf epots9) {
  4.           nonSillyPerson.ban(silly);
  5.           System.gc(); 
  6.      }
  7. }
Jun 14 '07 #10
epots9
1,351 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. TSDNAdmin nonSillyPerson = TSDN.getAdmin();
  2. for(SillyPerson silly : TSDN) {
  3.      if(silly instanceOf Jos || silly instanceOf epots9) {
  4.           nonSillyPerson.ban(silly);
  5.           System.gc(); 
  6.      }
  7. }
lol, that was good

lmao
Jun 14 '07 #11
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. TSDNAdmin nonSillyPerson = TSDN.getAdmin();
  2. for(SillyPerson silly : TSDN) {
  3.      if(silly instanceOf Jos || silly instanceOf epots9) {
  4.           nonSillyPerson.ban(silly);
  5.           System.gc(); 
  6.      }
  7. }
Ha! Code war!

Expand|Select|Wrap|Line Numbers
  1. TSDNAdmin nonSillyPerson = TSDN.getAdmin();
  2. for(SillyPerson silly : TSDN) {
  3.      if(silly instanceOf Jos || silly instanceOf epots9) {
  4.           nonSillyPerson.ban(silly= null);
  5.           System.gc(); 
  6.      }
  7. }
kind regards,

Jos ;-)
Jun 14 '07 #12
r035198x
13,262 8TB
Ha! Code war!

Expand|Select|Wrap|Line Numbers
  1. TSDNAdmin nonSillyPerson = TSDN.getAdmin();
  2. for(SillyPerson silly : TSDN) {
  3.      if(silly instanceOf Jos || silly instanceOf epots9) {
  4.           nonSillyPerson.ban(silly= null);
  5.           System.gc(); 
  6.      }
  7. }
kind regards,

Jos ;-)
Expand|Select|Wrap|Line Numbers
  1. public boolean ban (SillyPerson silly) {
  2.      if(silly == null) {
  3.          for(SillyPerson silly : TSDN) {
  4.                   if(silly instanceOf Jos || silly instanceOf epots9) {
  5.                    silly.defenestrate();
  6.              }
  7.          }
  8.      }
  9.      else {
  10.           silly.defenestrate();
  11.      }
  12.                System.gc(); 
  13. }
  14.  
Jun 14 '07 #13
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. public boolean ban (SillyPerson silly) {
  2.      if(silly == null) {
  3.          for(SillyPerson silly : TSDN) {
  4.                   if(silly instanceOf Jos || silly instanceOf epots9) {
  5.                    silly.defenestrate();
  6.              }
  7.          }
  8.      }
  9.      else {
  10.           silly.defenestrate();
  11.      }
  12.                System.gc(); 
  13. }
  14.  
Ha! simple stuff:

Expand|Select|Wrap|Line Numbers
  1. public boolean ban (SillyPerson silly) {
  2.      if(silly == null && silly != null) {
  3.          for(SillyPerson silly : TSDN) {
  4.                   if(silly instanceOf Jos || silly instanceOf epots9) {
  5.                    silly.defenestrate();
  6.              }
  7.          }
  8.      }
  9.      else {
  10.           (silly= r035198x).defenestrate();
  11.      }
  12.                System.gc(); 
  13. }
  14.  
kind regards,

Jos ;-)
Jun 14 '07 #14
r035198x
13,262 8TB
Ha! simple stuff:

Expand|Select|Wrap|Line Numbers
  1. public boolean ban (SillyPerson silly) {
  2.      if(silly == null && silly != null) {
  3.          for(SillyPerson silly : TSDN) {
  4.                   if(silly instanceOf Jos || silly instanceOf epots9) {
  5.                    silly.defenestrate();
  6.              }
  7.          }
  8.      }
  9.      else {
  10.           (silly= r035198x).defenestrate();
  11.      }
  12.                System.gc(); 
  13. }
  14.  
kind regards,

Jos ;-)
Incompatible types: found TSDNAdminNonSillyPerson required, SillyPerson (Jos)
Jun 14 '07 #15
JosAH
11,448 Expert 8TB
Incompatible types: found TSDNAdminNonSillyPerson required, SillyPerson (Jos)
Duh:

Expand|Select|Wrap|Line Numbers
  1. public interface Defenestratable {
  2.    public void defenestrate();
  3. }
  4. ...
  5. public class TSDNAdminNonSillyPerson implements Defenestratable {
  6.    ...
  7. }
  8.  
kind regards,

Jos ;-)
Jun 14 '07 #16
r035198x
13,262 8TB
Duh:

Expand|Select|Wrap|Line Numbers
  1. public interface Defenestratable {
  2.    public void defenestrate();
  3. }
  4. ...
  5. public class TSDNAdminNonSillyPerson implements Defenestratable {
  6.    ...
  7. }
  8.  

kind regards,

Jos ;-)
Argh, alright I give up. I'm cooked. There should not be allowed a reasonable implemention of the defenestrate method.
Next time I'll choose the pink axe first ...
Jun 15 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: bjam | last post by:
Hi, if I want to include a set of call template methods into my xsl file can I do that? Meaning I want to have the template method below of data_output in another file that I can use in several...
1
by: Julius Mong | last post by:
Dear all, if I have something like this: <html> <body> <embed src="..." id="svgPage" /> </body> </html> and I did this in the same doc:
4
by: JJ | last post by:
Hi, usually, I'm not using MS servers, but I have a big problem with a Access table. I should create a web application for a Historical Dipartment. They have created a populated a Access...
11
by: stu | last post by:
I have several databases that are opened using various versions of Access and VB. Up till recently everything worked fine, then I started getting a variety of lock file error messages, both on my...
10
by: Markus Svilans | last post by:
Hi, I have a weird problem in a virtual method. The original method code raises an access violation when it is run. The solution to the problem is to declare a dummy integer inside the virtual...
2
by: thread | last post by:
Hi All i'm building a database in access and i want to restrict permissions. from the access i can just limit the posiblity to unhide an hidden table and in this way i can preventing the users...
2
by: jpr | last post by:
I have 2 classes saved as 2 different java files.(eg- class ABC.java & PQR.java) ->ABC.java classABC extends JTextField { ABC(int i) { super(i); ...
1
by: bsonline | last post by:
I have to modify a .java file in a project. But I dont have any editor to compile java. I use putty to connect server(Solaris). And using putty console I modify .java file and save it. Then I down...
10
by: manjava | last post by:
I have a problem in my method I know how to continue to find the shortest distance and then extract cinques the nearest distance method must return cinques longitudes and latitudes with their cities...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.