0% found this document useful (0 votes)
15 views2 pages

Java Code Examples for Beginners

The document contains 5 sections of Java code. Section 1 defines two static variables and prints their values. Section 2 defines a final variable and prints its value. Section 3 defines a class with two instance variables and a method to set their values, then creates two objects and prints the variable values. Section 4 defines a class with two instance variables and a constructor to set default values, then creates an object and prints the variable values. Section 5 defines a class with two instance variables and a constructor that sets the variable values, then creates two objects passing different values to the constructor and prints the variable values.

Uploaded by

gogulkrishnaece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Java Code Examples for Beginners

The document contains 5 sections of Java code. Section 1 defines two static variables and prints their values. Section 2 defines a final variable and prints its value. Section 3 defines a class with two instance variables and a method to set their values, then creates two objects and prints the variable values. Section 4 defines a class with two instance variables and a constructor to set default values, then creates an object and prints the variable values. Section 5 defines a class with two instance variables and a constructor that sets the variable values, then creates two objects passing different values to the constructor and prints the variable values.

Uploaded by

gogulkrishnaece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

1 // Online Java Compiler

// Use this editor to write, compile and run your Java code online
import [Link].*;
class Hello
{
static int a=5,b=10;

public static void main(String[] args) {


[Link](a+" "+b);
}
}
2// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import [Link].*;
class HelloWorld {
final int a=10;
public static void main(String[] args) {
HelloWorld h=new HelloWorld();
[Link](h.a);
}
}
3
import [Link].*;
class Suresh
{
int a,b;

void input(int a,int b)


{
this.a=a;
this.b=b;
}

}
class Main{
public static void main(String args[])
{
Suresh s=new Suresh();
[Link](10,20);
[Link](s.a+" "+s.b);
[Link]();
Suresh s1=new Suresh();
[Link](100,200);
[Link](s1.a+" "+s1.b);
}
}
4
import [Link].*;
class Suresh
{
int a,b;

Suresh()
{
a=5;b=6;
}

}
class Main{
public static void main(String args[])
{
Suresh s=new Suresh();
[Link](s.a+" "+s.b);
}
}
5
import [Link].*;
class Suresh
{
int x,y;

Suresh(int a,int b)
{
x=a;
y=b;
}

}
class Main{
public static void main(String args[])
{
Suresh s=new Suresh(10,20);
[Link](s.x+" "+s.y);
[Link]();
Suresh s1=new Suresh(100,200);
[Link](s1.x+" "+s1.y);
}
}

You might also like