Tag Archive for static

Java Tutorial #16 — Introduction to methods


Topics Discussed: Methods, passing by value, method arguments, return types, void

Source Code Available Here


Methods
Methods are the primary way of breaking a program into individual component processes and is the key behind a concept known as Modularization. The general idea behind modular programming is breaking a large program down into smaller, more manageable parts; as this makes programming quicker, and debugging far more direct. On the other hand, excessive modularization can make for excessively verbose source code and difficult breaks in logic.


Using Methods
The syntax for using methods is pretty straightforward and will be described in depth below:


(access modifier)(static / non)(return type)(name)(argument list){
     //payload of method
}

Now let’s break the above example into smaller, more manageable parts:

  • Access modifier: The access modifier tells the compiler what can / can’t access the method, we’ll get more into this soon
  • Static / Nonstatic: This is fairly trivial, but it’s worth mentioning that you can’t directly call a non-static method from a static interface (calling a non-static from main for example).
  • Return Type: The return type is the type of value the compiler can expect to send back to the interface that you “called” the method from
  • Name: The name of the method can have the same name as any variable (doesn’t violate codespace, etc.)
  • Argument list: When the method is called, it can be passed variables(we’ll get more into this later) that can then be used locally in the method. The argument list tells the compiler what types of variables to expect and what to name them internally in the method

A few examples of methods and their respective calls would be as follows


//Example 1:

//Placed outside of main
public static void displayItems(){
     System.out.println("Items go here");
}

//placed inside main
displayItems();

//Example 2:

//Placed outside of main
public static void multiplyNumbers(int numberOne, int numberTwo){
     System.out.prtinln("The product of the two numbers is: " + (numberOne*numberTwo));
}

//Placed inside of main:
int a = input.nextInt();
int b = input.nextInt();

multiplyNumbers(a,b);

//Example 3 -- with return type

//Placed outside of main
public static int multiplyNumbers(int numberOne, int numberTwo){
     return (numberOne*numberTwo);
}

//Placed inside of main:
int a = input.nextInt();
int b = input.nextInt();

int product = multiplyNumbers(a,b);
     System.out.prtinln("The product of the two numbers is: " + product);


Return types
Return types are used to set a value of to be used in some process inside of the interface you called it from (for now that will most likely be main). This isn’t just limited to int, this can be any of the normal types that we’ve discussed so far, and even objects when we get to that point later on. So this could be boolean, string, a 1/2 dimensional array, etc. On the other hand Void means that absolutely nothing will be returned to the interface in which the method was called. This is useful when the program doesn’t need the values anymore, as they are destroyed when the method ends.


Passing variables, and the argument list
The argument list defines what we can / cannot pass to our method. There will be times when this is great for debugging, since you might have a variable that is waiting for 5 items, but only gets 4, the compiler will tell you that it’s awaiting the 5th. Also, very important to note is that up to this point we have been passing variables into our method by value, that means that the contents of our variable are being sent over, not the variables themselves. Therefore, any changes to our variables that might take place in the method only “count” in that method, here’s an example:


     public static void prodSum(int x, int y){
        //x = j,  k = y
        System.out.println("The product is: " +(x*y)); // produdct of our numbers
        System.out.println("The sum is: " +(x+y)); // produdct of our numbers
        x +=25;
        y+=55;
    }
     

           System.out.println("Enter number 1: ");
           j= input.nextInt(); // input 10
           System.out.println("Enter number 2: ");
           k=input.nextInt(); // input 20
           
           sum = prodSum(j,k);
           System.out.println("The sum is: "+ sum);

           System.out.println("j: " + j); // will still equal 10
           System.out.println("K: " + k); // will still equal 20

In the coming lessons we’ll be introducing how to pass by reference, which is where we will be passing the variable itself (sort of) to the function for extra processing.

Java Tutorial #18 — Access Modifiers; Working with / around them


Topics discussed: Access modifiers, public, private, static
Source Code Available Here


Access modifiers
Access modifiers are our way of sectioning off our program and making it harder to accidentally use / misuse methods not contained within the calling class. There are 4 access modifiers and they basically work like this:

  1. Public: Public as an access modifier means that the ANYTHING can call on your class / function. This means that anyone / any function can call it for any reason, we’ll see why that’s a problem later on.
  2. Protected: Protected is the exact same as public except that it cannot be called by anything, only things that exist in the same package / class / subclass as where the call originated from.
  3. not specified: Only accepts calls from the class / package and no subclasses.
  4. Private: Only accepts calls from within the class.

  5. Using public as an access modifier
    When we use public as an access modifier, the entire program (or any other program) can call upon our methods / objects if they are familiar with our interface. We might get into external calls at a much later lesson but for now let’s look at internal same-project calls:

    /////  Javafile.java /////
    
    int x= 1, y=7;
    //the syntax to call a public method in another file is methodFile.methodName
    sum = methodFile.sum(x,y)
    
    /////methodFile.java/////
    
    public static int sum(int x, int y){
    return (x+y);//Will return the sum of x+y to Javafile.java
    }
    


    Using private as a modifier
    If you’re in doubt about whether or not you should be using private as a modifier, you should be using private as a modifier. When we get into larger programs in the future with 4 or 5 different files, data will be moving all over the place, and controlling the flow of that data is extremely important. The only way to ensure that nothing gets passed incorrectly is to safeguard methods / classes that we want to keep safe.