1

I'm at very early stages of learning java.

I currently have one file which has 900 lines of code, which is not good.

I would like to separate my Java file into separate java files which I could "link" together in some way.

I used to work a lot with CSS, and there it was insanely easy to pull some stylesheets in with the @import.

Would there be a way to "copy" all variables from a file to a different one, the @import way? If I had my array "p" defined in the separate file, could I just @import this file into my main file in a way where I could just try to print p and it would do it?

Thanks a lot in advance!

Jonáš Vacek
  • 103
  • 2
  • 9
  • 1
    You need to think about iy in OO way. Do not think about files. Think about classes and objects – talex Oct 07 '14 at 16:06
  • Would you please elaborate? I haven't really covered objects just yet. – Jonáš Vacek Oct 07 '14 at 16:08
  • I'm somewhat curious too, what makes you think 900 lines of code isn't good? If it all actually makes sense to be together, there's nothing wrong with 900 lines of code in one class. – user2366842 Oct 07 '14 at 16:08
  • If you haven't covered objects yet, then with the greatest of respect, your time is best served reading a good Java book before you try to go any further. – Graham Borland Oct 07 '14 at 16:09
  • [This book](http://www.amazon.com/Java-Illuminated-Active-Learning-Approach/dp/1449632017) taught me everything I know, and I still use it as a reference. – AdamMc331 Oct 07 '14 at 16:20

4 Answers4

2

You cannot split a single class into several different source files. Each source file in Java corresponds to exactly one class. (A class can contain its own inner classes, but that's an unnecessary distraction at your stage!)

You can arrange things so that a member of one class is visible to members of other classes, by giving it the public or default access modifier.

See In Java, difference between default, public, protected, and private

Community
  • 1
  • 1
Graham Borland
  • 57,578
  • 20
  • 131
  • 176
  • Great answer. I would also mention for OP (not sure what their knowledge is) to be aware of packages and how they work as well. If the new files is also in a different package, it will need to be imported. This is also where the 'protected' variable really comes into play. – AdamMc331 Oct 07 '14 at 16:13
  • So if I have my variable "p" in a separate file with its public class "Unique", given it's in the same directory, how could I use this variable? – Jonáš Vacek Oct 07 '14 at 16:15
  • It depends on whether it's an instance variable or a static field. Seriously, please spend some time reading a book. This is really fundamental stuff which you really need to understand before you go further. – Graham Borland Oct 07 '14 at 16:17
  • Well, if it's a public static variable (something that is unchanging) you can simply call it with the class name and period: `Unique.p`. Typically, class variables are private, so you will need an object reference and an accessor method, which would go something like this: `Unique u = new Unique(); int p = u.getP();` [here's information on accessor and mutator methods](http://java.about.com/od/workingwithobjects/a/accessormutator.htm) I will try to find you a link of the Java book I use as reference. It's where I learned everything, and I believe it is very helpful. – AdamMc331 Oct 07 '14 at 16:18
2

You will probably want to read up about Object Oriented Programming. There are lots of tutorials on Oracle's website. http://docs.oracle.com/javase/tutorial/java/javaOO/index.html

In your case, you can split your classes up to store your array in one class and access it from another class. Something like this:

Here's the class that contains the array:

public class ClassA
{
    public int [] arr = {1,2,3,4};

    public int [] getArr()
    {
        return arr;
    }
}

Here's the class that accesses the array:

public class ClassB
{
    public void printArray(ClassA classB)
    {
        // access the array through a "getter" (recommended)
        System.out.println(Arrays.toString(classB.getArr()));

        // you can also access the array directly since it is a public variable
        System.out.println(Arrays.toString(classB.arr));
    }

    public static void main(String [] args)
    {
        ClassB a = new ClassB();
        ClassA b = new ClassA();
        a.printArray(b);
    }
}

If your classes are in different packages, you include an import statement at the top of the file like:

import packageName.ClassA;

Amber
  • 2,365
  • 13
  • 18
1

You might want to consider splitting the class if you think it has low cohesion. Take a look at SOLID

Some of the principles listed here might help you break down the class in to smaller manageable highly cohesive units.

Neo
  • 3,868
  • 4
  • 36
  • 49
0

I imagine you have something like this currently where everything is in one big class:

public class Main1 {
    private String street;
    private String city;

    public Main1(String street, String city) {
        this.street = street;
        this.city = city;
    }

    private void printAddress() {
        System.out.println(street);
        System.out.println(city);
    }

    public static void main(String[] args) {
        Main1 ex = new Main1("1 Main street", "Springfield");
        ex.printAddress();
    }
}

As a first step I would move some of the data out of your class into another class, grouping like data together (in this case it's obviously an Address) and then reference that class in your original class, maybe:

class Address {

    private String street;
    private String city;

    public Address(String street, String city) {
        this.street = street;
        this.city = city;
    }

    public String getCity() {
        return city;
    }

    public String getStreet() {
        return street;
    }
}

public class Main {

    private void printAddress(Address address) {
        System.out.println(address.getStreet());
        System.out.println(address.getCity());
    }

    public static void main(String[] args) {
        Main ex = new Main();
        Address address = new Address("1 Main street", "Springfield");
        ex.printAddress(address);
    }
}

After that you might notice that some of the functionality might also logically belong on the class you just pulled out and you can provide methods on the new class to provide that functionality:

class Address {

    private String street;
    private String city;

    public Address(String street, String city) {
        this.street = street;
        this.city = city;
    }

    public String formattedAddress() {
        return street + "\n" + city;
    }
}

public class Main {

    public static void main(String[] args) {
        Main ex = new Main();
        Address address = new Address("1 Main street", "Springfield");
        System.out.println(address.formattedAddress());
        //ex.printAddress(address);
    }
}
rainkinz
  • 9,042
  • 5
  • 34
  • 65