9/1/2023 0 Comments Package learn java book![]() ![]() So now I'll be the one to tell you that that's the case, I won't assume you know this ) The first thing you'll see in any Java file is the package declaration, but what's tricky is that most Java tutorials online or in books leave this declaration out (simply because they assume that you know what a package is). And yes, it has to be at the top, otherwise your code won't compile properly. That's all there is to it, since the Vehicle Class file exists inside of the “” package, it needs to have that declared at the very top. code removed for the sake of learning about packages only! So, in the case of my Vehicle Class file, the code will look like this: Any Class file you've created that exists inside of a package needs to have the declaration of that package at the very top of the file. What do Java packages look like in code?Īlright, so now that you have a good understanding of what these packages are all about, let's see how they translate in code. Again, just a convention, but it makes it easier for any new programmer to jump into your code and understand everything with relative ease. So my website is … so in turn, the packages I would create are “”. They don't have to be, but it's just how everyone else does it, so you might as well be like the rest of the programming herd! Also, another convention is that if you're developing a Java project for a website, you generally name your packages after the website address in reverse order. You just need to type in your packages names separated by periods (.) like so:īy convention, package names should be entirely lowercase. Piece of cake, all I did to create these packages was right click on my “src” folder (or the root HowToProgram folder) and choose New -> Package. If that makes sense to you, then your next question would probably be “Now that I understand WHAT a package is, HOW do I create them in MY project?“. So, what you see above is essentially the equivalent of having the following directory structure in your file system: ![]() Window -> Show View -> Package Explorer (or Other, then type in Package Explorer).If you don't see this in your Spring IDE, then try clicking on the following menus: I'm glad you asked, let's take a look at Spring STS and they way they display packages:Īlrighty, so here we see that Spring STS has a “Package Explorer” view. Anything bigger than that (let's say 5 Class files or more), then it might make sense to start creating packages. You can get away with it if you have a tiny program, such as a simple Java tutorial program. But this is kind of like storing all your music files and pictures right onto your desktop without organizing them into folders… it's just messy. It is used to “ package” together all of your files into one logical place! It's really just that simple boys and girls :) Why are Java Packages Usefulĭon't get me wrong, it's entirely possible to not use any packages at all and just shove all of your project's files into one spot. ![]() ![]() So the way to think of a Java package, is like a folder in your regular Windows/Mac file system. As a project grows and grows, we'll inevitably have more and more Class files (.java files). Well folks, Java packages are the means by which we organize our code. What is a Java package? Why is it useful? What does it look like in code? These are the questions I'll be answering today! What is a Java Package Welcome back folks, today's Java tutorial will cover the Java package. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |