classes within java code example
Example 1: classes in java
public class MathTest {
public static void main(String[] args) {
class MathAdd {
int num1;
int num2;
public int addNumbers() {
int addThemUp = num1 + num2;
return addThemUp;
}
}
MathAdd addition = new MathAdd();
addition.num1 = 10;
addition.num2 = 20;
System.out.println(addition.addNumbers());
}
}
Example 2: 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!!!");
}
}
}