java classes and objects programs code example
Example 1: java class
// Example : TestClass (Can be predefined or user-defined)
public class TestClass {
// properties
private int id = 111;
// constructor
public TestClass(){
super();
}
// method
public void test(){
// some code here
}
}
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!!!");
}
}
}