Skip to Main Content

Methods in Java

N_RajJul 9 2015 — edited Jul 9 2015

Hi,

I am new bee to Java.

I got the below program from Java Class Example | Java Examples - Java Program Sample Source Code.

/*
Java Class example.
This Java class example describes how class is defined and being used in Java language.
Syntax of defining java class is,
<modifier> class <class-name>{
  // members and methods
}
*/
public class JavaClassExample{
  /*
  Syntax of defining memebers of the java class is,
  <modifier> type <name>;
  */
  private String name;
  /*
  Syntax of defining methods of the java class is,
  <modifier> <return-type> methodName(<optional-parameter-list>) <exception-list>
{               ...
  }
  */

  public void setName(String n){
    //set passed parameter as name
    name = n;
  }

public String getName(){
    //return the set name
    return name;
  }

  //main method will be called first when program is executed

public static void main(String args[]){
    /*
    Syntax of java object creation is,
    <class-name> object-name = new <class-constructor>;
    */
    JavaClassExample javaClassExample = new JavaClassExample();
    //set name member of this object
    javaClassExample.setName("Visitor");
    // print the name
    System.out.println("Hello " + javaClassExample.getName());      
  }
}

The program has 4 methods including Main method.( Main,Name,getName,setName).

Why they used 4 methods?

Why can't use all the things in main method? or can we use Main method plus another custom method? It is reducing code.

Why we use  class-constructor when we create new objects?

Could you explain about  System.out.println.What is system / out / println ? Like  Schema.Table.column name?

Similar - What is the meaning of org.apache.hadoop.mapred?

Thanks,

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Aug 6 2015
Added on Jul 9 2015
2 comments
514 views