Fundamentals of Programming in Java
Fundamentals of Programming in Java
The keywords const and goto are reserved but not used.
• class MyJavaProgram
• {
• public static void main(String args[])
{
System.out.println(“Have fun in Java…”);
}
• }
• Understanding first java program
• Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().
• class keyword is used to declare a class in java.
• public keyword is an access modifier which represents visibility, it means
it is visible to all.
• static is a keyword, if we declare any method as static, it is known as
static method. The core advantage of static method is that there is no need
to create object to invoke the static method. The main method is executed
by the JVM, so it doesn't require to create object to invoke the main
method. So it saves memory.
• void is the return type of the method, it means it doesn't return any value.
• main represents startup of the program.
• String[] args is used for command line argument.
• System.out.println() is used print statement.
•
• System.out.println is a Java statement that prints the argument passed, into
the System.out which is generally stdout.
• System is a Class
• out is a Static Member Field
• println() is a method
• System is a class in the java.lang package . The out is a static member of the System
class, and is an instance of java.io.PrintStream . The println is a method of
java.io.PrintStream. This method is overloaded to print message to output destination,
which is typically a console or file.
• class System {
• public static final PrintStream out;
• //...
• }
the System class belongs to java.lang package
class PrintStream{
public void println();
//...
}
the Prinstream class belongs to java.io package
Compiling and Executing Java Program
Step-1: Save your file with .java extension.
• Example: Program1.java
NOTE: If the class is public then the file name MUST BE same as
the name of the class.
Step-2: Compile your .Java file using javac compiler from the
location where the file is saved.
javac Program1.java
Compiling and Executing Java Program
java MyJavaProgram
Access Specifiers
Specifier Sub class Non-Sub class Sub class Non-Sub class
(Same (Same (Different (Different
Package) Package) Package) Package)
private No No No No
Important Points
A user defined outer class can be either public or default. We
can not define a private or protected class.
1.class Student{
2. int id;//field or data member or instance variable
3. String name;
4.
5. public static void main(String args[]){
6. Student s1=new Student();//creating reference
to object of Student
7. System.out.println(s1.id);//
accessing member through reference variable
8. System.out.println(s1.name);
9. }
10.}
Object and Class Example: main outside class
1.class Student{
2. int id;
3. String name;
4. }
5.class TestStudent1{
6. public static void main(String args[]){
7. Student s1=new Student();
8. System.out.println(s1.id);
9. System.out.println(s1.name);
10. }
11.}
Initialize object
• 3 Ways to initialize object
• There are 3 ways to initialize object in java.
1. By reference variable
2. By method
3. By constructor
1) Initialization through reference
18. } }
Initialization through constructor