objects vs classes

objects vs classes

The biggest thing to remember about objects and classes is that a object is a instance of a class. People commonly say that a class is a blueprint, and a object is a specific iteam made off of that blueprint. Another anology is that a class is a page in a coloring book, and a object is that coloring page fully colored, with the variables defined in the constructor being the colors.

An example of this is making a string:

string example = “object”:

So String is a class, and “example” is a object.

Some things to keep in mind is that their are static methods and variables and those belong to the class so if one object of that class changes a static variable, then that variable is changed for all the objects of the class during that runtime.

For example(in Java):

https://codehs.com/share/CountingObjects_sAXefQ

public class MyProgram
{
public static int numObj=0;
public int var=0;
public static int counto()
{
numObj++;
return numObj;
}
public int count()
{
var++;
return var;
}
public static void main(String[] args)
{
//static methods don’t have the object name in the call to the method because the object doesn’t //matter
MyProgram one = new MyProgram();
System.out.println(“these are the result of static variables”+ “\n”+counto());
// so numObj is 0 then 0+1=1
MyProgram two = new MyProgram();
System.out.println(counto());
//so numObj is 1 then 1+1=2

// non-static have the object name or this because it does matter
System.out.println(“These are the result of non-static” +”\n” +two.count()+ ” “+two.count() );
// so var =0 currently for object two ,so 0+1=1 then var now = 1 so 1+1=2
System.out.println(one.count()+ ” “+one.count() );
// so var =0 currently for object one so 0+1+=1, ten var now =1 so 1+1=2

}

}