What is object in java ? (I am starting student so help me with example ) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is object in java ? (I am starting student so help me with example )

Every object in java is called an instance. but I don't know object in java please explain with an example

27th Oct 2017, 5:51 AM
ajmal yousuf
ajmal yousuf - avatar
4 Answers
+ 32
for example ::: class is "vehicles" , then object of this vehicle class will be scotter,car,truck, bike etc //btw here is the java course , see comments after every lesson ... all things were explain there ☺ https://www.sololearn.com/Course/null/?ref=app //hope it helps ☺
27th Oct 2017, 6:26 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 7
import java.util.Scanner; Scanner scn = new Scanner(System.in); /*Here i have created an object from class Scanner, the "new" keyword creates an object of that class which is called "an instance of class".*/
27th Oct 2017, 7:28 AM
D_Stark
D_Stark - avatar
+ 1
Object is an instance of a class! Take the class Person as an example: public class Person{ public int age; public String name; public Person(String name, int age){ this.name=name; this.age=age; } public static void main(String[] args){ Person mark = new Person("Mark", 20); System.out.println(mark.age); System.out.println(mark.name); } } // So mark is an object of the class Person which means that he has the attributes that class has for each object in this case age and name. And as they're public you can access them just like: //mark.age or mark.name
27th Oct 2017, 7:18 AM
Chriptus13
Chriptus13 - avatar
+ 1
Take a class to be a Grade 2 class in a school right. Lets assume there is a computer in Grade 2 that only Grade 2 students can use. So the objects(Grade 2 students) can access the computer in Grade 2 class. So an object is basically created from a class to access the resources or methods of that class, basically. For example: public class Grade2{ public static void main(String [] args) { Grade2 John = new Grade2(); John.Computer(); } public void Computer(){ System.out.println("Access Granted"); } } In this example, John is the object created from the class Grade2 and John can now access any method in Grade2 class, and in this case, John has access to Computer().
27th Oct 2017, 9:52 AM
Amos Aidoo
Amos Aidoo - avatar