0

new Java programmer here. I have some experience with C/C++, but something that I'm finding troubling with Java is that your class name must be the same as your file name.

My professor wants us to name our files according to the specific homework assignment (i.e. CH03_12 for chapter 3, program #12) However I'm finding it increasingly difficult to function when I'm having to use that as my class name.

I emailed him about the problem, and he replied with "Keep CH03_12 for the class/file name driver which manipulates the Invoices." Anyone who can help me better explain what he's saying would be of great help. :)

Thanks!

Em Lock
  • 21
  • 2
  • its possible he means you just to keep to his naming convention for the main class (the one with the main() method) and youre free to create inner classes / other classes and name them as you wish? – radai Aug 29 '13 at 16:48
  • I think that's what he means, but I'm unsure of how to do that in Java. Any pointers? lol – Em Lock Aug 29 '13 at 16:55

1 Answers1

1

He's telling you to keep his file naming format. As to the requirement to have the class name match the file name, it's to make it easier to keep track of what does what.

I suspect your instructor is telling you to create a main driver class by his naming convention, then implement the rest of your code in intelligently-named class files, called from main? ie -

public class CH03_12
{
    public static int main(String[] args)
    {
        Foo foo = new Foo();
        foo.bar();
    }
}

then in Foo.java file

public class Foo
{
    public void bar()
    {
        //do something
    }
}

Note that if Foo.java is in a different package than your main class, you'll have to use an import statement to tell the main class about it.

StormeHawke
  • 5,987
  • 5
  • 45
  • 73
  • I think that would be okay? This is the first time we've had to submit an assignment that requires a main driver, so it's a bit confusing for me. How do you call in a class file in Java? – Em Lock Aug 29 '13 at 16:54
  • In my example, if Foo.java is in the same folder as CH03_12.java, then you just reference it. IF you put it in a subfolder, you'll need to use `import subfolder.Foo` just below the `package` declaration in the class `CH03_12` – StormeHawke Aug 29 '13 at 16:59
  • You could also reference this question for more detail on package structure: http://stackoverflow.com/questions/1953048/java-project-structure-explained-for-newbies – StormeHawke Aug 29 '13 at 17:02