i was reading somewhere that said that objects in java are called classes..I am kind of confused because I thought java has BOTH objects and classes. Someone please explain
They are distinct terms.. objects and classes -- however they have a relationship.. once you understand that relationship, the terms will make sense. Within a java program, there can be multiple classes.. however there is only one main method- which I'm sure you're familiar with. From that main method, you create 'objects' of the other classes that you've made. An object holds data.. information. It can hold what you want it to hold. Though such a thing is best shown, not explained. Let's pretend I have a java program that holds info about a person.. their name, age, and activites... I'll have 2 classes - a main class which houses the main method -- and a secondary class which I will make an object of to hold information about a person. ``` public class PersonInformation { private String name; //holds data about person's name private int age; //holds data about person's age private String[] hobbyList; //holds data about their hobbies public void setAge(int ageNum) { age = ageNum; } public void setName(String nameVal) { name = nameVal; } public void setHobby(String[] hobbies) { hobbyList = new String[hobbies.length]; for(int I = 0; I < hobbies.length; I++) { hobbyList[I] = hobbies[I]; } } public int getAge() { return age; } public String getName() { return name; } public String[] getHobby() { return hobbyList; } } ``` Now here is our main method-- we will create multiple objects of the PersonInformation class, and you will see how it acts. ``` public class MainPerson { public static void main(String[] args) { //now I'll create an object of the PersonInformation class PersonInformation Sally = new PersonInformation(); //The class is of PersonInformation -- Sally is the actual object Sally.setName("Sally"); //I call the setName method of the PersonInformation class //to change the data of the Sally object Sally.setAge(65); //the age data of the Sally object is now 65 String[] hobby = { "Penguin tamer", "Muscle model", "Poet" }; Sally.setHobby(hobby); //now, here Is a concept: You can have multiple objects of the same class // and each object holds different data from another object PersonInformation George = new PersonInformation(); George.setName("George"); George.setAge(12); String[] Ghobby = { "fireworks", "bonfires", "marshmallow-eating" }; George.setHobby(Ghobby); //now we have 2 objects of the PersonInformation class.. time to display them System.out.println(Sally.getName() + " is " + Sally.getAge() + " years old."); System.out.println(Sally.getName() + " enjoys:"); String str[] = Sally.getHobby(); //this is fancy code for displaying an array for(int I = 0; I < str.length; I++) { if(I == str.length-1) { System.out.println(str[I] + "."); } else { System.out.print(str[I] + ", "); } } //array is displayed - now to format a line to separate the first //object from the second vvv System.out.println("==================================="); //now let's print out the data of the George object System.out.println(George.getName() + " is " + George.getAge() + " years old."); System.out.println(George.getName() + " enjoys:"); str = George.getHobby(); //this is fancy code for displaying an array for(int I = 0; I < str.length; I++) { if(I == str.length-1) { System.out.println(str[I] + "."); } else { System.out.print(str[I] + ", "); } } } } ``` Hopefully this helps clear up some of the fog.. however if you're truly new to java - do not worry about this yet! First tackle making methods.. multiple methods within the one class, and calling those methods from the main method.. Otherwise, such topics like classes and objects wont make much sense, since it builds upon knowledge of methods. I will show you what the console output (console output is just text.. shown from System.out.println() - either in a command prompt or in a console window in an IDE like eclipse) looks like in an attached image below.
Awesome...i just finished my intro to java and had not understood this concept well...thanks so much...very clear explanation :)
Glad it helped :P. I remember classes and objects being a hard concept to grasp when I was first getting into OOP. As you can imagine - there's a lot one can do with making multiple classes.
classes to object is like blue print of department , you can build many department with that blue print with different structure another example Class Car{ have BMW , Mercedes and all brand of car \\ this are object of class car but have different characteristics .
Join our real-time social learning platform and learn together with your friends!