aspose file tools*
The moose likes Beginning Java and the fly likes Call a method from another class!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Call a method from another class!!" Watch "Call a method from another class!!" New topic
Author

Call a method from another class!!

Nathan Wallace
Greenhorn

Joined: Apr 24, 2003
Posts: 4
I was wondering if anyone could help me out?? I am trying to figure out how to call the hello() method from another class. Here's the code I have to work with.
public class Out {
public static class In {
public void hello() {
System.out.println("Hello World!");
}
}
}
And when I put this code in Jbuilder 8, I get the following error message: Class Out is public; must be declared in a file name Out.java at line 1.
Any help would be appreciated.
Les Hayden
Greenhorn

Joined: Jun 19, 2002
Posts: 28
Nathan,
The under certain conditions java compiler requires the name of the class and the name of the source file to match. a public class is one of those times. Another one is when you have a main method in your class.
In your case, the Out class will have to be in a file named Out.java
The issue is really a little more complex than that, but not much.
Les
[ April 24, 2003: Message edited by: Les Hayden ]
[ April 24, 2003: Message edited by: Les Hayden ]

Les Hayden
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

Actually the "rules" are fairly straight-forward: your file must be named the same as the public or package-protected (default access) class defined within it, and must reside in a folder structure that matches your package structure. It may seem a bit odd when you are coming from another language, but becomes natural after just a little practice.

Another one is when you have a main method in your class.

I'm not sure where Les was going with that, but unless I am mistaken, having a main method imposes no special naming rules on the file other than that covered by any other class.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Augustin Caine
Greenhorn

Joined: Apr 24, 2003
Posts: 8
Save the source code of your Out class in a file called "Out.java".
Then in the other class, just make an instance of your Out class and call the method.
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
I thought that you could only have one public class per source file. So yes, then you would have to make another source file callled Out.java and put that class in there.


Thanks, leo
Nathan Wallace
Greenhorn

Joined: Apr 24, 2003
Posts: 4
Is there any way of doing this without adding the Out.java class?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Originally posted by leo donahue:
I thought that you could only have one public class per source file. So yes, then you would have to make another source file callled Out.java and put that class in there.

It is true that you can only have one public top-level class in a source file; but in this case the In class is nested inside the Out class.
Try this:

file: Out.java

file: Test.java

Your file must be named the same as the top-level public class in the file (Out.java and Test.java in the above examples).


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
Is there any way of doing this without adding the Out.java class?

[CODE]
Yes, you can do it,combine the two classes in one file,but the action is not recommanded to do it.
And save it to the first class name.
One thing I want to say is that Java is "case sensitive".


Francis Siu
SCJP, MCDBA
Nathan Wallace
Greenhorn

Joined: Apr 24, 2003
Posts: 4
Well my new code is this:
public class App{
public static class In {
public void hello() {
System.out.println("Hello World!");
}
}
public static void main(App[] args) {
In.hello();
}
}
and somehow I need to call the hello method, but when I do, it tells me that Non-static method hello() cannont be referenced from a static context.
I'm not really understanding this whole java thing and it is really confusing. Thanks for the help so far, but any additional help would be great.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Your method hello is not declared as static, and as such needs an instance of the object in order to call it. (See my above post for an example.)
However, unless you are doing something specific (usually GUI related), the creation of inner classes is not a common thing. You can (and are probably better served to) create separate App.java and In.java files.
Nathan Wallace
Greenhorn

Joined: Apr 24, 2003
Posts: 4
Well, I've tried everything I can think of and used the help from the posts. I just can't seem to figure it out. The question reads: Based on the following class definition, code the statement(s) needed to call the hello() method from another class.
public class App {
public static class In {
public void hello() {
System.out.println("Hello World!");
}
}
}
I've tried the examples that were posted here. Maybe I'm just doing something wrong. I understand that I can't reference it cuz it's a non static method, but I can't figure it out.
Augustin Caine
Greenhorn

Joined: Apr 24, 2003
Posts: 8
Given that declaration, I tried it out. Here's what I got. This should acces the method you want.

public class App {
public static class In {
public void hello() {
System.out.println("Hello World!");
}
}
}

public class Apptest {
public Apptest() {
}
public static void main(String[] args) {
App.In w= new App.In();
w.hello();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call a method from another class!!