Part 4: Objects

Sep 7, 2016
Part 4: Objects
  • Crash Course to Java

    Part 4: Objects



    Java is an Object Oriented Programming (OOP) language. This means that it is based on data and objects rather than simply running a set of actions. If you've ever been told not to 'static abuse', this is why. Since a class contains only the data it needs to access, the code inside a class won't be able to accidentally modify something it doesn't need to, which makes your code significantly more secure. There are, of course, other pros and some cons to the OOP paradigm. But we're not here to talk about that, so let's move on!

    Section A: Instantiation
    Object instantiation is creating an instance of a Class for the purpose of using/modifying/storing data using the variables and methods outlined in the Class and defined in the Object.

    When you instantiate an object, you use the object's 'constructor'. A constructor looks similar to a method, only it has the same name as the Class and has no return type.
    Example:
    Code (Java):
    public class Example {
        private String name;

        public Example(String name) {
            this.name = name; // Use "this" to access instance variables/methods from within the class
        }

        public static void test() {
            new Example("bob");
        }
    }

    Section B: Instance Variables vs Class Variables
    An instance variable is a variable that is a member of an instance of a Class (an object). You would use instance variables for things like fur color or name for a Dog class, so that if you create 2 dogs, they have 2 separate names and fur colors.

    A class variable, on the other hand, is associated with the Class itself. These are made using the static modifier. Depending on the access modifier, they can be accessed directly from the Class or from an instance of the Class, but either way they will have the same value or do the same thing.
    This example shows the difference:
    Code (Java):
    public class MainClass {
        public static void main(String[] args) { // You'll learn about this in part 5!
            OtherClass example1 = new OtherClass();
            OtherClass example2 = new OtherClass();
            System.out.println(example1.getInstanceString());
            System.out.println(example1.getClassString());
            System.out.println(example2.getInstanceString());
            System.out.println(example2.getClassString());

            example1.setInstanceString("one");
            example2.setInstanceString("two");
            example1.setClassString("dave");

            System.out.println(example1.getInstanceString());
            System.out.println(example1.getClassString());
            System.out.println(example2.getInstanceString());
            System.out.println(example2.getClassString());
        }
    }

    class OtherClass {

        private String instanceString = "Instance String!";
        private static String classString = "Class String!";

        public void setInstanceString(String string) {
            instanceString = string;
        }

        public void setClassString(String string) {
            classString = string;
        }

        public String getInstanceString() { return instanceString; }

        public String getClassString() { return classString; }
    }
    Output:
    Code (Text):
    Instance String!
    Class String!
    Instance String!
    Class String!
    one
    dave
    two
    dave
    Even though we only set example1's classString to "dave", since it's a static class member, both are changed.

    Continue the guide in Part 5!
  • Loading...
  • Loading...