Justin
Justin

Reputation: 105

Is source file and a package the same thing in Eclipse?

I am a complete beginner when it comes to Java. I recently picked up Head First Java and it says: "Put a class in a source file. Put methods in a class. Put statements in a method." When I open eclipse i started a new project called helloWorld, this created a project with a src folder(guessing this is the source file?), i then followed an eclipse tutorial from their website and it stated that i needed to first create a project, then a package, then a class in that package. What is the difference between a source file and a package?

Upvotes: 1

Views: 2669

Answers (3)

Joop Eggen
Joop Eggen

Reputation: 109547

The following is a java source text:

package org.apache.twinkle;

public class Elfie {
    ...
}

It resides under a sources directory (generally src), and has a file path:

org/apache/twinkle/Elfie.java

(Directories org, apache, twinkle and file Elfie.java.)

So a package indicates some hierarchy and corresponds 1:1 with a directory. The source file has a .java extension.

Paths should be case-sensitive. Package paths are hierarchical and generally follow the convention of starting with a reversed URL.

http://mit.com
package com.mit.mathlib.graphs;

http://univ-abu-dabi2.net
package net.univAbuDabi2.linguistics;
import com.mit.mathlib.graphs.GraphUtils;

Upvotes: 2

unigeek
unigeek

Reputation: 2826

A package more-or-less equates to a directory under your "src" folder in this case. Examples might include "com.project.ui" or "com.project.models" (and so there would be a "com" directory inside "src" and inside "com" you would have "project" and so on).

A source file is just that--it's an individual file that will live in one of those packages, probably named as "MyClass.java" where "MyClass" corresponds exactly to the name you give the one public class that the source file should contain.

BTW, if you will build your code with Maven, you should follow the suggested Maven directory structure--see this. In the case of Maven then, your java packages would start under "src/main/java" rather than under just "src" which is maybe what Eclipse will assume you want by default.

EDIT: Also take care to align the package you declare at the top of your Java source file with the package that it actually "lives in" on your filesystem--it's essential that these be in agreement. So, if your "MyClass.java" lives on the filesystem in com/projects/models, your package statement at the top of "MyClass.java" must be "package com.projects.models;" By convention package names will be all lowercase, class names will be upper and lower ("camel case") starting with a capital letter and method names start with a lowercase letter, but then are also camel case.

Upvotes: 3

roeygol
roeygol

Reputation: 5028

Source file is complete Java code.
Package gather a several Java file under some issue like: GUI, server, login and etc.
Try to create several package and then go to the workspace to see what you got.

Also, when it comes to package issues, you also have the 'package' definition for class variables, which means that you are able to use this variable from other classes in the same package.

Upvotes: 0

Related Questions