Results 1 to 3 of 3
  1. #1
    fartek is offline Member
    Join Date
    Feb 2009
    Rep Power

    Default Can I write code in 2 different files ?

    So Instead of writing all of my code in 1 java file. Can I write some pieces of code in different files and include them in the "main" java file ?

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Rep Power


    Think of it as classes, not java files. Unless very small, each java class needs to be in one java file. If you have very small classes, sometimes they may be grouped together in 1. But you cannot split one class into 2 java files, I can't really see what the point of that would be, then you would have to open another file to see the other part of your class. So the answer is No, but I'm curious as to why you even wanted to. Also, you're not being very specific with "code". If all your code consists of many classes, of course, give them each a separate file, but if it's only one class, leave it as it is.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Rep Power


    Yes... you can (and in some cases should) have your code in different classes. It should depend on the function of the methods in the classes. For example, you can have a "file" class and it has various methods related to file management (open, read, save, copy, etc). Then you may have another class that has methods that have to with input and output (user input and printing and logging).

    Here's a couple of links that may help:

    The Java™ Tutorials
    Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. What are you using to write your code?
    By CaptainMorgan in forum New To Java
    Replies: 1037
    Last Post: 03-20-2017, 06:03 AM
  2. Help me to write a secure code
    By Bhavis in forum Advanced Java
    Replies: 4
    Last Post: 01-21-2009, 06:06 AM
  3. [SOLVED] Read/write files using applets
    By coolFrenzi in forum Advanced Java
    Replies: 3
    Last Post: 11-17-2008, 10:45 PM
  4. How to write efficient maintainable code.
    By Zosden in forum Advanced Java
    Replies: 9
    Last Post: 05-01-2008, 04:48 AM
  5. How to write code comments
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts