Java Helloworld
Calcuator Demo
package com.oop;
class Calculator
{
public Calculator() {
System.out.println("I am
constructor");
}
//1.add 2.sub
3.multiplication 4.division
public int add(int num1,int num2)
{
return (num1+num2);
}
}
public class CalculatorDemo {
public static void main(String[] args)
{
Calculator
calcObj=new Calculator();
System.out.println("addition is
"+calcObj.add(2, 3));
}
}
Example
package com.oop;
class Human
{
private String address;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress()
{
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class HelloWorld {
//Type2:declaration
and oneliner init
//datatype varname="value"
static String empName="Ashwin
More";
//Type1: declaration and then intialization
//dataType variable Name;
//variableName="value";
//creation of objects
//ClassName objectName=new
className();
public static void main(String[] args) {
System.out.println("HI Java Batch
and i am "+empName);
Human
objectHuman=new Human();
objectHuman.setAddress("Pune");
objectHuman.setAge(23);
Human
objectHuman1=new Human();
objectHuman1.setAddress("Pune");
objectHuman1.setAge(20);
System.out.println(""+objectHuman1.getAddress());
}
}
//visibily modifier class
ClassName
0 comments: