Posts Tagged global declaration
Java Tutorial 13 – Visibility and Scope
Visibility is how accessible methods and variables to other class is. We’ve used methods from other class in tutorial Multiple Class. Which was possible because all the method we used from other class was public. There are mainly three types of access specifier.
- Public
Every Public variable and method is accessible to all other class. - Protected
Protected elements are inheritable, means each child class has all the protected method that parent has and it only accessible between all it’s inheritance tree. - Private
Private elements are accessible only for class itself.
Lets have a cup of Example:
we will create 2 class parent class will have 1 public, 1 protected and 1 private int, and we will try to access these ints in its child class.
Parent:
Java Tutorial 5 – Creating methods
We have used system defined methods like println() and nextInt(), Now let’s create our own method and use it.
Defining method is very useful when bunch of code is getting repeated, just put that code in a method and just call that method instead of writing all the code again
create a class and add this code as always we do.
- Why int and Scanner before main method?
if we create int inside the main method, then, only main method can use or change that int. so defining anything in the class and outside any method is called Global declaration.
- Why Static?
to use that int into the main or any other method because main itself is static so any variable used within it must be static and thats why we also crated all the methods static.
We created 3 methods and it can be defined anywhere in the class.Observe the out put and you will dig out everything.
u can create different methods of adding subtracting etc. and create calc.
Doubts invited,
Have a nice day, Peace.
