Java Basics: Packages

Thu, 12 Aug 2010

When I first toyed with Java a week ago I found it very confusing to use in an IDE since the default Project for a Java Application forces you to start with a Package. I now see you can create your own packages in the Source Packages Sub-folder if I want to group things together. I somewhat understand the benefits of packages, ie: non-conflicting libraries and organization, but I'll try not to study too much of this yet I still need to get used to the syntax.

I'm on about page 200 of my SCJP Study Guide for Java 5 book. A lot of the content of this book is not in chronological order for reading in my opinion, but it is a fantastic book that covers things in great detail. Much of the OOP concepts I already understand from PHP so I find the learning a lot easier. If I had not had prior experience in OOP this book would be unreadable!

I'm new to java so I know if I go back and read this in a few months it might seem embarrassing, but cut me some slack :P So here's my package. I have a folder called Person. And two sub-files, Person.java and Test.java.

Test.java
package Person;

public class Test {

    public static void main(String[] args) {
	
	Person jream = new Person("JREAM", 25);

	System.out.println("Name: " + jream.getName() + ", Age: " + jream.getAge());

    }

}
Person.java
package Person;

public class Person {

    private String name;
    private int age;

    public Person(String name, int age) {
	this.name = name;
	this.age = age;
    }

    public String getName() {
	return this.name;
    }

    public int getAge() {
	return this.age;
    }

}

So there you have it. I had some help on the SitePoint Forums to get started with a few questions. since these guys have been a great help for many years.