Started programming in Java a few weeks/months ago, but getting horribly confused about classes, objects, settter and getters and this?
This page is for you. If you're pushed for time you can treat this page as a test and just look at the answers after thinking about them for a bit.
However, for maximum benefit you should try to actually do the exercises. If you get stuck, peek at the answer and then try to do the exercise from memory.
Don't stress too much about the theory of Java; actually being able to do stuff is the important thing.
Create a Java program that simply outputs the text "Hello World" when you run it. Call your main class "Application".
In the same file as your main "Application" class, define a new class called "Person". You don't need to put anything in the class.
Now, right below where you output "Hello World" in your main method, create an object from that class.
Hint: although you can only have one public class in each file, you can create as many other classes as you want in there, just so long as you don't use the public keyword with them. Usually of course you put classes in their own files, but here we'll put our classes in the same file for convenience.
Modify your "Person" class to add a constructor. Make the constructor output the text "Constructor running!".
1. Modify the Person class so that it has another constructor, in addition to the constructor it's already got. Make this second constructor accept a parameter called name of type String.
2. Make this second constructor print the name parameter using System.out.println().
3. Finally, change your "main" method so that when you create the Person object, you pass in a name. Pass in your own name, in fact.
Modify your Person class so that it has a private instance variable called name of type String.
In your second constructor, set the value of the "name" instance variable using the "name" parameter that you pass in.
Remove the System.out.println()'s from the Person class.
Hint: OK, there's a problem here. In the constructor there are two variables called "name". One is a parameter to the constructor, the other is an instance variable. How do you differentiate between them? The answer is to use the keyword this.
This app doesn't produce much output, but we'll use what we've done in the next exercise.
Methods are a way of making subroutines part of objects (by defining them in classes of course).
Add a method to your Person class called "writeName". Make this method output the text "My name is " followed by the value of the name instance variable.
Invoke (in other words run or call) this method in your main method.
Your program output should therefore look like the following, assuming your name is "Topsy".
Hello World! My name is Topsy
In prehistoric times, get and set methods (also known as getters and setters) were known as mutators and accessors.
Add getName and setName methods to your class.
Modify your main method like this:
1. Use getName to retrieve the name String from the Person object (in the main method, remember!). Store it there temporarily.
2. Add a space followed by your surname to the String you just retrieved.
3. Use the setName method to set the name instance variable of your person object to the full name that you've just created.
Do all this just before calling writeName().
Hint: none of this involves editing the Person class! All your editing should take place in main.
Your program output should now look like this (assuming your surname is "Delores", and why wouldn't it be):
Hello World! My name is Topsy Delores
Finally, the challenge to end all challenges. This last question is the bad guy at the end of the level.
1. Above where you defined the person class, define a class called "Brain".
2. Give the Brain class a constructor.
3. Make the Brain constructor print the text "Thinking...".
4. Give the Person class a private instance variable of type "Brain", called "brain".
3. In the one-parameter Person constructor, set the brain instance variable equal to a new Brain object.
Your program output should now look like the following:
Hello World! Thinking ... My name is Topsy Delores
Once you're able to do this kind of stuff from memory, you've got a decent grasp of the absolute basics of classes and objects in Java. Of course there's lots more stuff to learn, but this is a great foundation to work from.