Computer Science, asked by 278raghav, 5 hours ago

Create a Java statement to create an object ‘samsung’ of the class named ‘MobilePhones​

Answers

Answered by Diabolical
1

Answer:

Explanation:

Input :  

           public class MobilePhones {         // class MobilePhones

                int price = 2500;

               public void function() {

               System.out.println("I will perform calculation and other task for you.");

  }

                public static void main(String[] args){

                MobilePhones samsung = new MobilePhones();  // samsung object in_stantiation

                System.out.println(samsung.price);

                samsung.function();

   }

}

Output : 2500

              I will perform calculation and other task for you.

How it worked : As always we need a class in java file to create something of use and performing. So this time that class was MobilePhones. Now for an object to be created we need a class which we created already (give yourself a pat on your back, from me, as you have created your class.)

We inst.antiate an object to be a type of the class. That's the reason why we put class name (which is MobilePhones, in this case) before the object's name. After class's name we give the object a name (which is samsung in this case). Then we use new keyword to create the ins.tance of the class and thereafter we finally put class name again with parenthesis to describe some sort of runtime thing (which I won't bother to explain here. So, go get your brain troubled in finding the reasons behind using class's name twice). Since ins.tance of a class is called object, we created our object, here (do give another pat on your shoulder for this achievement of yours).

Since object is used to get the behaviors and states of the class, I used it to retrieve some info. (which I declared already in class as shown in figure : Input) about the class in the main method. That's why we are getting those inputs. You can do something else as per as your requirement.

That's all.

Attachments:
Similar questions