Java Tutorial for Beginners C03L02 Classes and methods


Video is ready, Click Here to View ×

In this tutorial we take a look at classes and a rudimentary look at what methods are. The key thing to take away from this tutorial is a rudimentary understanding of what methods are as we will be building on what you learn today in the next set of chapters.

18 thoughts on “Java Tutorial for Beginners C03L02 Classes and methods

  1. Lastly better to stick that example in an empty .java file in eclipse and hit control shift and f to format so you can actually read it. I hope this answers your question if it does not simply reply with any further questions and I will see if we can't clear them up for you.

  2. *When I say storing I mean holding data within the program for some sort of processing, actually storing your data persistently is normally achieved through csv, xml or much more commonly databases.

  3. As you can see this class called Main has a main method and getters and setters. One thing to note is that when I set the example variable I am setting the variable for the instance of Main called anotherMain. If this confuses you that is ok because you would rarely do something like this, it is merely a demonstration of how you can use getters and setters within a class with a main method..

  4. public class Main {

    private String example;

    public static void main(String[] args) {

    Main anotherMain = new Main();
    anotherMain.setExample("setting the example");



    public String getExample() {
    return example;

    public void setExample(String example) {
    this.example = example;


  5. Lastly it is not essential that you remove the main method to be able to generate getters and setters, what is essential is that your variables are declared as private and are outside of your main method as demonstrated below:

  6. i.e. you actually have to start the main method to get your ‘car’ moving where as classes with no main method simply respond to the state of your car ie should they be asked to do something they will however not without that engine being turned on.

  7. So if I may let me try to get you to think about it like this: your project contains many classes but in my experience normally only 1 main method, try to think of classes without main methods as vital parts of a car however the part of the car that actually allows the car to move forward and do things is primarily the engine; which in our project is the main method

  8. The getters and setters function as a means to access private variables from outside of it’s class.

    Ok so on to your question as to why we do not need or want a main method within the animal class; as you can see from what I wrote above the animal class is a way of modeling and storing data within your program and this is it’s function*

  9. I do not want to write a class with the same values in it more than once so I start with the animal class; the animal class allows me to store all information on an animal that is common to all animals.

  10. Hi nf767 hopefully I can clear this up for you.

    The animal class is there to act as a base for storing information on any animal we should want to create in our application.

    So from a real world perspective let's say I was tasked with creating a program able to store information on every animal in the world.

  11. Hi Sam,

    I'm very new to Java, apologies for any naivety accompanying my post. I was wondering why there is no main method written in the animal class? But I found that to use the 'Generate getters and setters', it is essential to remove the public static void main. Am I understanding something wrong here?..

  12. @sreezvideo I am glad you have found them useful, I will be continuing with these tutorials however I am still debating on whether to make a few more videos centered around oop examples or move on to threads. It is rather hard to construct a structured set of tutorials when you are faced with such large topics as oop is. Any comments on what you would like to see would be most welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *