• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call a method from another class!!

 
Nathan Wallace
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Augustin Caine
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nathan Wallace
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way of doing this without adding the Out.java class?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Nathan Wallace
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic