Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Separation of Concerns: How does it work?

How do the different files in a Java app like the Hangman game work? So you have the 3 files and it references an object or method from one file in another file.

I know you have your different access level modifiers like your public and private but I can't see any obvious thing that says objects are available in another file like an import statement or export like you get in JS.

How do you know which file to import packages to and which one is for one of a better expression a "parent" to another file in a directory?

Hope this is all making sense :)

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Hey Jonathan,

Are you still using workspaces? Or, are you at the point in the Track where you are using an IDE?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Still on workspaces, I've just started the Data Structures course :)

2 Answers

Jeremiah Shore
Jeremiah Shore
31,168 Points

"I can't see any obvious thing that says objects are available in another file like an import statement or export like you get in JS."

If you are using an IDE, such as IntelliJ IDEA, it will suggest other classes that are available on your classpath. It will do this in the form of an autocomplete; this can be manually triggered with the keyboard shortcut CTRL + SPACE.

"How do you know which file to import packages to and which one is for one of a better expression a "parent" to another file in a directory?"

It can help to think of packages as directories or folders themselves, because they are. However, you're going to need the full package name a class resides in and the name of the class to be imported if you want to use it without having to use a more, or fully qualified name (see below).

A good answer for why we import classes is as follows, taken from a StackOverflow question: "Meaning of the import statement in a Java file".

"The import statement in Java allows to refer to classes which are declared in other packages to be accessed without referring to the full package name. You do not need any import statement if you are willing to always refer to java.util.List by its full name, and so on for all other classes."

No one wants to type com.teamtreehouse.app.model.SomeClass every single time they refer to SomeClass, hence the need for an import statement of import com.teamtreehouse.app.model.SomeClass so it can be used as just SomeClass. Without using either importing or fully qualified names, the java compiler won't know what class you're talking about and it will throw a compiler error.

edit - It seems I missed the original question concerning separation of concerns, which wasn't much elaborated. I recommend reading more about the MVC design pattern. Each class should should have a certain use or particular purpose; otherwise can clutter your classes and make them more difficult to understand.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Jeremiah already gives a nicely detailed explanation. The reason I had asked if you were in Workspaces still, is because most of what you've asked about will become much more apparent and clear once the Track moves into IntellijIDEA. In Workspaces, much of what is imported or how things are accessed across the project aren't readily apparent, but they will be in the IDE. Just give it a little time... The thing to remember with Java, is you are working in a "Project" and everything is a part of and accessible in that "Project." But, like I said, it will become much more clear once you start working in the IDE. :)

:dizzy:

Jeremiah Shore
Jeremiah Shore
31,168 Points

Exactly. Despite possible confusion with command line compiling/execution, manual importing, etc. it is beneficial in the long run to understand how things work manually. I recommend doing what you can to understand the core pieces of how everything is put together in a small project while working in workspaces. IDEs take care of a lot of the work, but you will need to troubleshoot your IDE from time to time in most scenarios, and that knowledge helps.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

So I'm thinking a little too far ahead just that the moment. :) Okeydokey I'll go with that for now. Can't wait to get into the IDE's. That said, this is the first time, in all my time with Treehouse I;ve actually made proper and sustained use of Workspaces. I have plenty of them but I always prefer following along on my own text editor :-)

Jeremiah Shore
Jeremiah Shore
31,168 Points

LOL, you're going to love it then. Your background shows mostly CSS and JavaScript experience... I honestly don't know how people get by with simple text editors. I love Sublime Text for what I need it for, but IDEs bring out the real power behind working with a strongly typed/object-oriented languages.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I use notepad++.

I just can't get comfortable using any other text editor and I'm not on a mac so I'm seldom able to follow on exactly like in the videos which are mostly run on macs. So it'll be interesting to see if I can adapt to a workflow with IDE's