Well its the final day of Javatober and what better way to finish things off would be to revisit classes. Its been a crazy month of Java and I thought I would have a lot more to show for it, but to be honest with you, I am looking forward to continuing my Java journey and it has definately given me the motivation to continue. I think CodeGym has had a lot to play in that and I think the content alone will have me continuing to work and striving to understand the languange as in depth as possible.
For the last time....
What Did I Learn Today
The basis of Java is that programs consist of various types of objects(classes), where different classes contain different internal structures(variables and methods). Java progams use objects as building blocks, so when we ned a new object type, we create a new class and define the way its internal objects behave. As we've said previously, a class consists of methods which do things, and variables, which are used by the methods to store data. When writing classes, we also need to decide what variables different methods will share, and we take them out of the method and put them in the class itself. In other words we turn local variables into member (instance) variables.
So to create a class, we would:
Decide what objects we need
Divide the objects into different types, depending on what they do
Write a separate class for each type
Declare the needed methods and variables in the class
Write the code to make the method do what it needs to do
We can then use the class to create objects of that class
Objects Have Two Interesting Properties
Each object stores a copy of its instance variables, this means that changing variables in one object doesn't affect the variables in other objects.
When creating objects, you can pass different arguments. These values are used to initialize the object. Many classes require such arguments in order to create instances (objects) of the class.
Java Packages
Studying Java consists of two big tasks, learning how to write your own classes and learning how to use other people's classes, this is where Java packages come into things. We package our code as files which are grouped into folders. Ever Java class is stored in a separate file, which is then grouped into packages, which correspond to folders on the hard drive. A classes full unique name consists of its package name plus the class name:
For Example:
Full unique name Package name Class name
java.io.FileInputStream java.io FileInputStream
You can use a classes short name in your code intead of the full unique name, but at the beginning of your class you must indicate which classes you will use, with a line similar to 'import java.io.FileInputStream;'
It is always better to place classes into packages as well, and not the src folder. It is easy to manage when you don't have many classes, but when you have a lot more classes, it is east to mix them up.
Code For The Day
Its easy to create objects. You write the keyword new and then the name of the class that you want to create an object of.
The name variable is an instance variable and has a public access modifier making it visible anywhere in the project. The getName method returns the value of the instance variable name. The setName method and is used to assign a new value to instance variable name. The parameter has the same name as the instance variable, so we precede the name of the instance variable with this. If we don't want anyone to change the value of an instance variable, we can just not create a set method for it or we can make it private. We can also add additional data checks to the method. If the passed new value is invalid, nothing will be changed.