java classes and objects
class Dog {
int age = 5;
public static void main(String[]args) {
Dog myObj = new Dog();
System.out.println(myObj.age);
}
}
java classes and objects
class Dog {
int age = 5;
public static void main(String[]args) {
Dog myObj = new Dog();
System.out.println(myObj.age);
}
}
using class in java
public class HelloWorld {
public static void main(String[] args) {
class Number{
int number;
public boolean isPos(){
if(number > 0){
return true;
} else {
return false;
}
}
}
Number myNumber = new Number();
myNumber.number = 7;
if(myNumber.isPos()){
System.out.println(myNumber.number + " is positive!!!");
} else {
System.out.println(myNumber.number + " is not positive!!!");
}
}
}
using class in java
public class HelloWorld {
public static void main(String[] args) {
// how to use class in java
class User{
int score;
// method that can be used outside the class
public boolean hasWon(){
if(score >= 100){
return true;
} else {
return false;
}
}
}
User dave = new User();
dave.score = 20;
System.out.println(dave.hasWon());
}
}
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;
how to make a class in java
public class Main {
public static void main (String[] args) {
System.out.println("Hello World");
}
}
java classes/objects
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj1 = new Main(); // Object 1
Main myObj2 = new Main(); // Object 2
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}
}
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