Skip to main content
gamedev.net
🔒 Locked

[java] multiple files (WARNING: NEWBIE QUESTION)

Started by docyoung83 6 replies 300+ views
Original Post
docyoung83
docyoung83
I'm trying to create a simple Java project using more than one file. I ran the following and it worked, but is there a better way to do this? ********* FILE 1 ***************
public class File1
{
    public static void main(String[] args)
    {
        System.out.println("Inside File1");
        
        File2 f2 = new File2();
        
        f2.display();
    }
}
  
*********** FILE 2 *****************
public class File2
{
    public File2()
    {   }
    
    public static void display()
    {
        System.out.println("Inside File2");
    }
}
  
The output is: Inside File1 Inside File2 Our teacher hasn't gotten as far as multiple files yet, and my Java books don't talk about them. Thanks. [edited by - StonieJ on March 1, 2003 3:46:30 AM]
docyoung83
docyoung83
I honestly don''t know. Our teacher has taught us to add "public static" to the beginning of every function we write, with the promise that he will "explain it later."
brianlevine
brianlevine

Stonie, in general, it''s bad form to call a static method from an object instance. You''d be better off making the call to display as File2.display(). This makes it explicit that you''re calling a static method not an instance''s method. It won''t change the execution of the program, but it makes it easier to read/maintain.

hth.

/B
LNK2001
LNK2001
quote:
Original post by StonieJ
Our teacher has taught us to add "public static" to the beginning of every function we write, with the promise that he will "explain it later."

Your teacher probably has some good reason for telling you to do this "while learning". Sounds like a very dubious pedagogical trick if you ask me (But then again, I''m not a teacher). Using only static methods is definitely NOT good practice in the Real World(tm).
"-1 x -1 = +1 is stupid and evil."-- Gene Ray
TSwitch
TSwitch
While my intro CS lectures didn't do this, my friend's professor DID instruct him to make all methods static. This was because his class wasn't into OOP as quickly as we were, and his professor probably felt that telling them about something like that before they were ready (had gotten enough basic basic programming experience) before he went ahead and dumped more on them wasn't a good idea. (They had a hell of a problem just reimplementing 'cal'.)

BTW, his CS class was a "non-majors" version of my class because he was undecided when he started. He's since changed to mechanical and aerospace engineering. Too bad...

As for multiple files in Java, well, get used to using piles of them. A visual level editor for arkanoid (breakout) that I'm working on is well over 10 files without even doing ANYTHING yet. Nothing. If your Java books really don't get into multiple files, then they're either expecting you to pack all those classes into one file (something I've only seen done once in my life) or are completely skirting the issue. Which would be a bad book to use, since "piles of files" is some sort of Java mantra.

[edited by - TSwitch on March 4, 2003 9:31:30 AM]
Member of the Unban nes8bit or the White Rhino in my Basement Gets Sold to the Highest Bidder Association (UNWRBGSHBA - Not accepting new members.)Member of the I'm Glad Mithrandir Finally Found an Association that Accepts People with his Past History Association (IGMFFAAPPHA)
MaximuS_X
MaximuS_X
Your approach is definately the best way to go. You can place both files into one single file, however, this can get pretty messy when you have several in one. All you would have to do is put the source of both files into one and only have the class the contains the main() method declared as public and name your file according to that class'' name.
---
mstein
mstein
i could be totally pulling this out of my ass, but i remember in school that whenever we wrote java applications, we made everything static, but when we did applets we didnt. Im sure there is more to this that i knew then, but thats just what i remember. there was a reason though . . .

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.