This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Referencing a method form another class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Referencing a method form another class?" Watch "Referencing a method form another class?" New topic
Author

Referencing a method form another class?

Jack Davis
Greenhorn

Joined: Apr 07, 2003
Posts: 22
Hi, how do you Reference a method from another class? What are the steps invovled. Will it work if I say classname.methodname() ??
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
heyas,
firstly create an instance of that class

Example myExample = new Example(); //creates a new object called myExample
myExample.methodExample(); //uses the obejct reference myExample to call the method called 'methodExample'
hope that helps,
-twans


B.C.S.T, SCJP, Hero
Jack Davis
Greenhorn

Joined: Apr 07, 2003
Posts: 22
thanks for the reply, I tried it and although it doesn't give me an error it doesn't work. Should I make methods and objects public?
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
presumably if you were attempting to access a private method illegally the code would not complile correctly.
possibly post your code and i can have a look through it..
=)
twans
Jack Davis
Greenhorn

Joined: Apr 07, 2003
Posts: 22
Thanks. It on J2ME. I'm try to organise my classes in such a way that it makes maintainence easier. In this case I'm trying to access the form in this class form another class.
Hope it's not too big for you

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class subscribe extends MIDlet implements CommandListener {
private Display mainDisplay;
private Command backCommand = new Command("Back", Command.EXIT, 1);
private Command submitCommand = new Command("Submit", Command.OK, 1);
private Command mainCommand = new Command("Main", Command.OK, 1);
// the Subscription From
Form subscribe;
TextField name;
TextField number;
TextField email;

Traffic traffic = new Traffic();
Form succsub;
public subscribe() {
initsubscribe();
mainDisplay = Display.getDisplay(this);
}
protected void startApp() {
//Display.getDisplay(this).setCurrent(lists);
mainDisplay.setCurrent(subscribe);
}
protected void pauseApp() {}
protected void destroyApp(boolean u) {}


// Initialize the "keywordScreen" screen
public void initsubscribe() {
subscribe = new Form("Subscribe");
name =
new TextField(
"Enter full name:", "", 20, TextField.ANY);
number =
new TextField(
"Enter number:", "", 20, TextField.NUMERIC);
email =
new TextField(
"Enter email:", "", 20, TextField.EMAILADDR);
subscribe.append(name);
subscribe.append(number);
subscribe.append(email);

subscribe.setCommandListener(this);
subscribe.addCommand(submitCommand);
subscribe.addCommand(backCommand);
}

public void initsuccsub() {

succsub = new Form("Subscribed");

succsub.append("You have succesfuuly subscribed!");


succsub.setCommandListener(this);
succsub.addCommand(mainCommand);
}
public void commandAction(Command c, Displayable d) {
Screen screen = (Screen) d;

if(screen == subscribe)
{
if(c == submitCommand) {

initsuccsub();
mainDisplay.setCurrent(succsub);
}

else if(c == mainCommand) {
traffic.initlists();
mainDisplay.setCurrent(traffic.lists);
}

else if(screen == succsub)
{
if(c == mainCommand) {

traffic.initlists();
mainDisplay.setCurrent(traffic.lists);
}
}

}
}

}
Jack Davis
Greenhorn

Joined: Apr 07, 2003
Posts: 22
if you like I can post the other class that integrates with this one
Jack Davis
Greenhorn

Joined: Apr 07, 2003
Posts: 22
Acually i think I recognise the problem. It's a security issue, it won't let me create an instance of another class in this class.
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
and what lines were you concerned about.. when you posted your original question?
can you copy/paste the code which you think is not working..
also, just as a suggestion, if you want to use
"[" "code" "]"
write your code here..
and "[/" "code" "["
without all the quotations then it might help the readability
-t1
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
if the class is public, i cant see why there is any reason that you cannot initiate it..
and like i said, with the compiler, if you were trying to access/achieve an illegal task, it would not compile.
maybe if you could post the package and/or the interface its extending.
do you get any errors at all at runtime? does it do anything at runtime?
Jack Davis
Greenhorn

Joined: Apr 07, 2003
Posts: 22
when I include Traffic traffic = new Traffic(); in the subscribe.java file it seems to cause a problem - where the midlet ceases to work
Jack Davis
Greenhorn

Joined: Apr 07, 2003
Posts: 22
This is the other class invovled. Btw it compiles with error

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Traffic extends MIDlet implements CommandListener {
public List lists;
private TextBox tb;
private Display mainDisplay;

private Command SelectCommand = new Command("Select", Command.OK, 1);
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command unsubCommand = new Command("Unsubscribe", Command.OK, 1);
private Command backCommand = new Command("Back", Command.EXIT, 1);
private Command submitCommand = new Command("Submit", Command.OK, 1);
private Command mainCommand = new Command("Main", Command.OK, 1);
Form unsubscribe;
Form succunsub;
public Traffic() {
initlists();
mainDisplay = Display.getDisplay(this);
}
protected void startApp() {
//Display.getDisplay(this).setCurrent(lists);
mainDisplay.setCurrent(lists);
}
protected void pauseApp() {}
protected void destroyApp(boolean u) {}

public void initlists() {
lists = new List("Main Menu", List.IMPLICIT);
lists.append("Subscribe", null);
lists.append("Unsubscribe", null);
lists.append("Traffic Alerts", null);
lists.append("Route Planner", null);
lists.addCommand(SelectCommand);
lists.addCommand(exitCommand);
lists.setCommandListener(this);
}

public void initunsubscribe() {
unsubscribe = new Form("Unsubscribe");

unsubscribe.append("Chosing unsubscribe will deactivate your account!");


unsubscribe.setCommandListener(this);
unsubscribe.addCommand(unsubCommand);
unsubscribe.addCommand(backCommand);
}
public void initsuccunsub() {

succunsub = new Form("Unsubscribed");

succunsub.append("You have successfully unsubscribed!");


succunsub.setCommandListener(this);
succunsub.addCommand(mainCommand);
}

public void commandAction(Command c, Displayable d) {

Screen screen = (Screen) d;
if(screen == lists)
{

if(c == SelectCommand)
{
if(lists.getSelectedIndex() == 0)
{
subscribe sub = new subscribe();
sub.initsubscribe();
mainDisplay.setCurrent(sub.subscribe);
}

else if (lists.getSelectedIndex() == 1 ) {
initunsubscribe();
mainDisplay.setCurrent(unsubscribe);
}

}


else if (c == exitCommand ) {
destroyApp(false);
notifyDestroyed();
}
}

if(screen == unsubscribe)
{
if(c == unsubCommand) {

initsuccunsub();
mainDisplay.setCurrent(succunsub);
}
else if(c == backCommand) {
initlists();
mainDisplay.setCurrent(lists);
}
}
else if(screen == succunsub)
{
if(c == mainCommand) {

initlists();
mainDisplay.setCurrent(lists);
}

}

}

}
"[/" "code" "["
the error occurs when the program enters this section. i/e when I select this option from the list
if(c == SelectCommand)
{
if(lists.getSelectedIndex() == 0)
{
subscribe sub = new subscribe();
sub.initsubscribe();
mainDisplay.setCurrent(sub.subscribe);
}
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
the midlet ceases to work because your calling a constructor of another class before the subscribe constructor even begins. you see how you have called:
Traffic traffic = new Traffic();
in the instance field area of the class..
where/when do you want the traffic object to be called?
you might want to define
Traffic traffic;
where you have 'Traffic traffic = new Traffic(); '
and then when you want to create the traffic object, and have its constructor called have
...
trafic = new Traffic; // calls constructor here..
although i have only recieved drips and drabs this seems to be the most logical mistake..
May i ask if there is anything in the Traffic constructor, and what 'traffic' does?
-twans
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
sorry, my last reply was before i saw your most recent post.
what error occurs? is it at runtime?
what error messages occur when trying to compile the Traffic.java?
Jack Davis
Greenhorn

Joined: Apr 07, 2003
Posts: 22
The traffic constructor is empty. Bascially the traffic class is the main menu of the application. It provides a list. When I select subscribe from the list I want the subscribe class to kick into action.
I understand where you pointed out that error. To be honest though, I haven't got that far, i.e got a far as invoking that class. I think I should have posted you the traffic code first - it's here where the error occurs.
For what it's worth here's the error
java.lang.SecurityException: Application not authorized to access the restricted API
at com.sun.midp.security.SecurityToken.checkIfPermissionAllowed(+40)
at com.sun.midp.security.SecurityToken.checkIfPermissionAllowed(+7)
at com.sun.midp.midletsuite.MIDletSuiteImpl.checkIfPermissionAllowed(+8)
at com.sun.midp.midlet.MIDletState.<init>(+66)
at javax.microedition.midlet.MIDletProxy.<init>(+5)
at javax.microedition.midlet.MIDlet.<init>(+13)
at subscribe.<init>(+4)
at Traffic.commandAction(+38)
at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+284)
at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)
Jack Davis
Greenhorn

Joined: Apr 07, 2003
Posts: 22
Btw the traffic class compile ok too
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
okies, well first things first.
your correct in that the two classes depend on one another, and you should fix Traffic.java before attempting to work on subscribe. Now, i havn't worked with J2ME before, so my help somewhat becomes limited. Searching for the 'java.lang.SecurityException: Application not authorized to access the restricted API' error in google i saw that a suggestion to achieve success is to change the restrictions, allowing Traffic to access Subscribe.
sorry my knowledge doesn't go deep enough to help you with this, some possible area's of interest:
http://forum.java.sun.com/thread.jsp?forum=196&thread=468046&tstart=0&trange=15
(i think that one will help you remove your error)
http://www.coderanch.com/forums/f-41/JME
(that is java ranch's j2me site.. )
hopefully you get it all working! just give the permissions a go yourself from your initial link, and i suggest looking at javaranch's j2me as your problem is more specific to that topic..
best of luck
-twans
(by the way, it is common practice to define your class with a capital letter, ie public class Subscribe.. not subscribe )
=)
Jack Davis
Greenhorn

Joined: Apr 07, 2003
Posts: 22
thanks for your time and your help!!
Antoine Waugh
Ranch Hand

Joined: Jan 24, 2004
Posts: 66
no probs, hope the links help you create just what you want..
=)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Referencing a method form another class?
 
Similar Threads
Basic Question
System.out question
Static Methods
Can i call action class getRedirectUrl() method from another class?
Calling method from another class