java create new object
Car mycar = new Car();
java class
public class Lightsaber {
// properties
private boolean isOn;
private Color color;
// constructor
public Lightsaber(Color color) {
this.isOn = false;
this.color = color;
}
// getters
public Color getColor() {
return color;
}
public boolean getOnStatus() {
return isOn;
}
// setters
public void turnOn() {
isOn = true;
}
public void turnOff() {
isOn = false;
}
}
// Implementation in main method:
public class test {
public static void main(String[] args) {
Lightsaber yoda = new Lightsaber(green);
yoda.turnOn();
}
}
how to make an objec tjava
class Puppy() {
String name;
int age;
Puppy(String puppy_name, int puppy_age) {
name = puppy_name;
age = puppy_age;
}
public static void main(String[] args) {
Puppy rex = new Puppy("rex",4);
}
}
make an object in java
public class Puppy {
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public static void main(String []args) {
// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" );
}
}
using class in java
public class HelloWorld {
public static void main(String[] args) {
// how to use class in java
class User{
int score;
}
User dave = new User();
dave.score = 20;
System.out.println(dave.score);
}
}
object in java
Objects: came from class, we can create multiple objects from a class
ArrayList<> list = new ArrayList<>();
Class refName OBJECT
each object has its own copy of instance variable
· declared outside the blocks or methods
Object: Instance of the class. We can store different data's to objects
Object Creation: with new keyword.
ClassName obj = new ExistingConstructor;
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us