Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Package required in .java using Eclipse? (first project)

Hey all,
I hope this thread finds everyone well! I'm new to Java and this is literally my first project. The school I attend requires us to use Eclipse. When I start a new "class" (which please correct me if I'm wrong is about the same thing as a new java program inside a java project?) I am able to name the class (in example below it is bowtie) but the first line reads: "package yocumjavaproject1;"... if I take this line out there is an error. Although my program runs fine with this first line, in the example videos I have seen there has not been this line. I'm sure this is a simple thing but would like an explanation if possible as well as if there is a way to not need this first line. Thank you all again for your help!
1662144330469.png
 
Hey all,
I hope this thread finds everyone well! I'm new to Java and this is literally my first project. The school I attend requires us to use Eclipse. When I start a new "class" (which please correct me if I'm wrong is about the same thing as a new java program inside a java project?) I am able to name the class (in example below it is bowtie) but the first line reads: "package yocumjavaproject1;"... if I take this line out there is an error. Although my program runs fine with this first line, in the example videos I have seen there has not been this line. I'm sure this is a simple thing but would like an explanation if possible as well as if there is a way to not need this first line. Thank you all again for your help!
View attachment 1615
The name of your project is the name of the package. That means that you can export your project with that package, and import it to another project. This will allow you to be able to use the package's code in the other project
 
I have been doing mostly C# in recent years and as a result now I can't remember whether or not the package name is mandatory in Java. From what you write, it seems like it might be. But it would help if you specify exactly what error you are getting when you leave it out. Just writing "there is an error" does not give us much to go on, does it ? And btw, what is your problem with having to specify a package name ?
 
When I start a new "class" (which please correct me if I'm wrong is about the same thing as a new java program inside a java project?)
A class isn't a program inside a program, using your idea of one. A class is more of a template and an object is what you construct using your class; in other words: a class acts as a template to construct objects(hence, "object-oriented" programming).

Using what you've explained for your problem, Eclipse by default with generate a "default package" if there is no package attached to a Java project(this comes up as simply "(default)" or "(default package)" in your hierarchy). If however, you create a package and place your file underneath that package, Eclipse/the JDK(Java-Development-Kit) will recognize that file as being part of that package you've just created. So, by removing the package line from that file, you make it impossible for the JDK to recognize what package the file belongs to, and thus, you'll get an error.
 
A class isn't a program inside a program, using your idea of one. A class is more of a template and an object is what you construct using your class; in other words: a class acts as a template to construct objects(hence, "object-oriented" programming).

Using what you've explained for your problem, Eclipse by default with generate a "default package" if there is no package attached to a Java project(this comes up as simply "(default)" or "(default package)" in your hierarchy). If however, you create a package and place your file underneath that package, Eclipse/the JDK(Java-Development-Kit) will recognize that file as being part of that package you've just created. So, by removing the package line from that file, you make it impossible for the JDK to recognize what package the file belongs to, and thus, you'll get an error.
Hey, thank you for your help!! Sorry for the belated appreciation. I've noticed that I don't get "layman's" terms on most of what I ask; although to be fair, as much as I admire people that know this stuff, I can't seem to get a simplified answer. If you have time, please look at my other questions. Your answer was not only appreciated but easily understood. Thank you.
 
Hey, thank you for your help!! Sorry for the belated appreciation. I've noticed that I don't get "layman's" terms on most of what I ask; although to be fair, as much as I admire people that know this stuff, I can't seem to get a simplified answer. If you have time, please look at my other questions. Your answer was not only appreciated but easily understood. Thank you.
I've done a quick test to confirm that specifying a package name is not mandatory in Java. But here in Eclipse it is, as you have defined your source to be in this package. You did not answer my question about the exact error you get, but that seems a moot point now. Once again I wonder why you would want to get rid of that first line ?
 
Hey, thank you for your help!! Sorry for the belated appreciation. I've noticed that I don't get "layman's" terms on most of what I ask; although to be fair, as much as I admire people that know this stuff, I can't seem to get a simplified answer. If you have time, please look at my other questions. Your answer was not only appreciated but easily understood. Thank you.
You're welcome. I'm glad you found it easy to understand. Remember to always take your time learning and you'll understand eventually.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom