Here I continue my Java Video Tutorial by covering methods, or functions in Java.
I spend a considerable amount of time on those things that tend to confuse people who are learning Java. I review how class variables differ from local variables and provide numerous examples of methods.
If you missed the first tutorial see Java Video Tutorial, since I use a lot of code here that I explained previously. To help you better understand this video, see the code below.
If you like videos like this, feel free to share them
Code From the Video
import java.util.Scanner; // Scanner is a class that contains a bunch of methods public class LessonFive { // main is a method that contains all of the code to be executed when a program runs // This is a class variable that is available to every method // If you declare a variable in a method it is only accessible in that method (local variable) static double myPI = 3.14159265; // Any changes made to randomNumber in any functions will effect its global value static int randomNumber; // Creates a Scanner object that monitors keyboard input static Scanner userInput = new Scanner(System.in); public static void main(String[] args) { /* Basic Method * accessModifier static returnDataType methodName (parameters) * { Statements } * Access Modifier: Determines who can execute a method * static: Used when you want to be able to execute a method that isn't part of a class definition * Return Data Type: The data type of value returned after a method executes (void if no values are returned) * Method Name: Must start with a letter, but can include letters, numbers, $, or _ * Parameters / Arguments: Values passed to a method */ System.out.println(addThem(1,2)); // addThem(1,2) will be replaced with the value that method returns // Demonstrating passing by value int d = 5; // Changes to the variable d in tryToChange don't effect its value globally // We are passing the value of d to tryToChange and not the variable tryToChange(d); System.out.println("Static Variable d = "+ d); // Guessing a random number System.out.println("\n"); System.out.println(getRandomNum()); // Both prints and generates the value for randomNumber int guessResult = 1; int randomGuess = 0; while(guessResult != -1) { System.out.print("Guess a number between 0 and 50: "); // Accepts an integer input from the user // You can't declare this variable inside the while loop if you want to access it outside the while loop randomGuess = userInput.nextInt(); guessResult = checkGuess(randomGuess); } System.out.println("Yes the random number is " + randomGuess); System.out.println(randomNumber); // Random value was changed globally by getRandomNum } // Adds the two numbers sent and returns the solution // public is the access modifier and means anyone can execute this method // Java Methods can return any primitive data type, or reference to an object (More on that later) public static int addThem(int a, int b) { double smallPI = 3.140; // This variable is local to the addThem function // compare returns 0 if equal | -1 if smallPI is less than myPI | 1 if smallPI is greater than myPI System.out.println(Double.compare(smallPI, myPI)); int c = a + b; // return returns a value that replaces the call to this method // It must be an int since you defined this method returns ints above return c; } // When you define an attribute / parameter you must define its type // That's why you can't type tryToChange(d) // Because this function doesn't return a value return type is void public static void tryToChange(int d) { d = d + 1; System.out.println("tryToChange d = " + d); } public static int getRandomNum() { // Creates a random number between 0 and 50 // Since randomNumber is a class variable you don't have to declare, or define its type // If int randomNumber was declared in this method it wouldn't effect the global variable named randomNumber randomNumber = (int) (Math.random() * 51); return randomNumber; } public static int checkGuess(int guess) { if(guess == randomNumber) { return -1; } else { return guess; // Must return a value of type int } } }
Java Videos in the Series
Thank You for gave us wonderful tutorial of JAVA…
when will you upload next tutorial video on this site?
Thank you. I tend to bounce around a bit, but I’ll get the next one up in the next 2 days. I’m glad you like them π
hey thanks for this one also, your tutorials are awesome..
Thank you. Thanks for taking the time to show your appreciation
Enjoy your style of tutorial. Well done
Thank you. I’m glad you like the videos π
return 0 v/s return 1?
0 is considered false and 1 true
I love your tutorial. It is very helpful
Thank you π
i just loved ur tutorials…they r great…but i hav a que too…m not getting the concept of ‘static’…with varibles , method , and main method…
I think you may have asked this question on youtube, but either way I hope this helps. If not tell me and I’ll explain it further.
Static fields are tied to the class and not the objects that areο»Ώ created by a class. No matter how many objects you create there will only ever be one value for that static field. Static fields are normally constants or are used for tracking. Static methods are used to perform 1 operation. Like Math.random() is used to generate a random number. Static fields can’t be declared in main, or any method because they are class fields. Does that help?
Your definition of static is awesome..it added a lot of meaning to my knowledge of static…I now understand what static is!
I’m very happy to help π
and what if i declare the input varibale inside the main method…and if declare any varibale as ‘static’ but inside the main method..then would it not be accessible to other classes???
hei Derek,
i need to thank u a lot. .i hate programg languages,but ur videos made me to love it. .tey r super duper easy to learn things tat too,in a easier way. . kep ur work going. .
Thank you very much π I’m very happy that you enjoy them. Thank you for taking the time to tell me they have helped
*****************************************
public class Testing
{
private int speed = 10;
public static void testing (String[] args)
{
int speed_local = opening(); // this is not letting me go to the method
}
private int opening()
{
int s = speed;
return(s);
}
}
************************************
why can’t i go reference a non static method from a static one? how would i be able to access my private speed
You’d have to create an object of class Testing and then reference speed from it
Hi, Derek. I need your help. Please explain!
WHAT IS THE MEANING FOR THE UNDER BELOW DESCRIPTION???
WHAT IS RETURN -1 stand for???
if(guess == randomNumber)
{
return -1;
} else {
return guess; }
checkGuess() is checking if the guess and the randomNumber are equal. If they are it returns a -1 which ends the while loop. If anything other then -1 are returned the while loop continues to ask the user to guess again. Does that help?
yes,
Thanks a lot and sorry to bother you.
You’re very welcome π
you are excellent tutor!!
Thank you π That is very nice of you to say that.
Hello, Derek, do you believe in karma and Indian Vedic culture?
Sorry for my
russi english.When I was growing up my Great Grandmother believed that when you do bad things that they will eventually come back to haunt you. The same was true if you did good things. She referred to this as Karma, but she also considered herself to be a Christian.
I personally always try to follow this belief. I have read many religious texts, but I was also taught as a boy that the only rule we need to follow to be happy is to do to others what we would have them do to us. I think that has worked pretty good for me because I’m very happy today.
Hi Derek,
Thank you, greats tutorials, i love them.
Could you please post some exercises based on your tutorials so we can practice.
Hi Eran,
Thank you π I’ll see what I can do in the future. I plan on making free Android apps that will go along with these tutorials to help people practice. That is the major goal for this year.
Hi Derek. I’m new to learning Java. I’ve watched this video for like 4 or 5 times until I understand what is going on. Great video! I hope I can teach others about Java just like you someday. Besides Java, I can feel that there is something which stirs my heart in your video and this website. Perhaps it’s called passion. I appreciate it. You’ve made my world a bit more beautiful.
Thank you for the very kind message π I greatly appreciate you taking the time to writing it. Yes I love java as well.
Derek, Thank you for taking the time to be so thorough and thoughtful in your videos, comments and explanations.
When first undertaking the job of learning java, I thought it would be mere weeks to understand the concepts of the language. Not so, at least, for myself.
I have subscribed to various services and viewed hundreds of videos to determine that this will be a long-term learning process.
Upon discovering your tutorials my learning has markedly improved and I actually enjoy and look forward to your lessons. I can not say “Thank You” enough times for the difference you have made.
May the Lord bless you and your family.
Thank You,
Nick
Hi Nick, Thank you for the nice compliments. Yes there is a lot to learn to be a great programmer, but it is doable. Everyone eventually has a aha moment when everything starts to make sense. Mine came when I started studying object oriented design. Everything made sense then. I have a OOD tutorial here. Learning UML may help greatly as well. May God bless you as well π