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.

  1. Public
    Every Public variable and method is accessible to all other class.
  2. 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.
  3. 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:

public class Parent {
    static public int a;
    static protected int b;
    static private int c;
}
Child:
public class Child extends Parent {
    public static void main(String[] args) {
        a = 0;
        b = 0;
        c = 0;  // this line give you error
    }
}
We do not need to run it, just type it and when you mansion c=0 it will give you error because child can not inherit private elements from parent and so Child class doesn’t know the int c exist.
Scope:
Scope means where the method or variable is accessible in the same class.
for example if u define an int inside a for loop, this int is not accessible out side the for loop and Same thing is true for any method also.
if you have any variable inside a method than its only accessible inside it.
In short we can say elements are accessible between {…} inside which they are declared.
So, if you declare variable or method inside class ( outside other methods ) it’s accessible between {..} of class means inside whole class and so it’s called Global declaration.
Some Examples:
1) Global declaration
public class yash{
int a;           // accessible inside all methods of class
int b;           // accessible inside all methods of class
method1()
…..
…..
method10()
}
2) inside Method which is not accessible to other method
public class yash{
method1(){
int a;           // accessible only for method1
int b;           // accessible only for method1
}
…..
…..
method10()
}
I hope you cleared if not merriment declaring and using variables different places in class.
Doubts are invited.
have a nice day.
Peace.

, , , ,

Leave a comment

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.

  1. 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.

  2. 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.

, , , , , , ,

Leave a comment

Design a site like this with WordPress.com
Get started