-libraries/ packets on top / imports or whatever is needed//a class can be called to perform tasks over and over without having to do procedural programming (where the code is read one line at a time). This save programmers tons of time.
public class ninja { //public means it is accessible from anywhere
// local variables are placeholders of data in this local class called ninja.
String color; //string type means it will hold letters,number, and other things.
integer numNinja; //integer type means it can hold number positive or negative, but not decimals.
//constructor that is public(accessible from anywhere in the code).
//a constructor is a place holder for the methods being called in this class ninja
// methods are just code statements that perform an action.
public ninja(){
}//setter methods that set up what the variables will have
//void means they don't return a value, it only sets data into the two variables
//this method is called setColor
public void setColor(String _color){
color = _color;
}
//this method is called setNumNinja
public void setNumNinja(integer _numNinja){
numNinja = _numNinja
}
//getter methods that can be called anywhere in the code to get the data from the variables
//this method is called getColor
public String getColor(){
return color; //return statement
}
//this method is called getNumNinja
public Integer getNumNinja(){
return numNinja; //return statement
}
^^^^everything is set now you want to be able to use this class. Here is how it is done:
//create an instance of an object. in this case it is called ninja. lets create some real ninjas in code.
ninja n = new ninja();
^^^this statement creates the object n on the left side. on the right side the new instance of class ninja is created. n object equals a new class ninja instance
//now call the methods from ninja class
n.setColor = ("black"); //the ninja object has been set to "black"
n.setNumNinja = (6); //the ninja object has been set to 6
//now for something to actually happen. You can use the object code to output the data in many ways. I will just show a simple system out statements.
System.out.println(n.getColor()); //prints out "black"
System.out.println(n.getNumNinja()); //prints out 6
Now you can see that many objects can be created each doing their own actions in a program. It is all about the design in Object Oriented Programming (OOP). This is only the basics.