0
Q:

function in java

//declare a function like this:
void function(int parameter1)
{
	//function body
}
//call the function like this:
function(1);
4
public static void main(String[] args)
{
	int length=getLength();
  	System.out.println("Your length is: "+length);
}
//methods are outside of main method
public static int getLength()
{
	int length=5;
  	return length;
}
5
public class MyClass {
  private int value = 10;
  // getter method
  public int getValue() {
    return value;
  }
  // setter method
  public void setValue(int value) {
    this.value = value;
  }
}
3
public int addNum(int num1, int num2) {
  total = num1 + num2;
  System.out.println("Total: " + total);
}
0
public class Main
{
    static void function(){
        System.out.println("I am a function!");
    }
	public static void main(String[] args) {
		function();
	}
}
0

New to Communities?

Join the community