Question

Here I want to access that protected variable rollno to my another package package2. So guys I have put both programs: So here when I run Check.java it throws an error not defined @ Protected1 the only objective here is to access the protected variable from package test to package2

package test;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Protected1
{
    static protected int rollno;
    static public String name;
    static private int mobileno;
    public void Protected1()
    {
        System.out.println("lets start doing the Assignment for Protected variable                                                                 assigning in another package");
    }

    public static void main(String a[])
    {

        /*protected int rollno;
        public String name;
        private int mobileno;*/
        Scanner br=new Scanner(System.in);
        //Protected1 st=new Protected1();
        try
        {
            System.out.println("Enter the name:");
            name=br.nextLine();
            System.out.println("Enter the rollno:");
            rollno=br.nextInt();
            System.out.println("Enter the mobile number:");
            mobileno=br.nextInt();
            System.out.println("lets just print it out:");
            System.out.println("name is \t"+name);
            System.out.println("rollno is \t"+rollno);
            System.out.println("mobileno is \t"+mobileno);
        }
        catch(Exception e)
        {
            System.out.println("Exception caught");
            e.getMessage();
        }

    }
}


package package2
import java.io.*;
import java.util.*;
import test.* ;

public class Check extends Protected1 
{
    public int Check()
    {
        System.out.println("hello guys ..lets try"); 
        return 0;
    }
    public static void main(String a[])
    {
        Scanner dr=new Scanner(System.in);
        Protected1 as=new Protected1();
        int roll=as.rollno;
        System.out.println("type welcome");
        String input=dr.nextLine();
        System.out.println("now we have to assess the protected variable from Protected 1 i.e"+roll);
    }
}
Was it helpful?

Solution

Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.

Read more: Here

A static variable is one that’s associated with a class, not objects of that class. Static variables is initialized only once.

OOP Languages are supported "static" keyword and their properties but many OOP programmers think that static variables or static methods should not be used in programs. One reason of that,static variables are stored in Stack.Then if so many static variables are used in program,stack is fulling.

OTHER TIPS

The short answer: you cannot access protected variable from other package. This is the reason that java (and other object oriented languages) have access modifiers.

However there is exception for (almost) each rule. If you really need this you can create special accessor class in that package that accesses the "protected" variable (or method) or extend the class that contains the protected member and exposes it via public method.

Other way is to use reflection. You can always call setAccessible(true) on every method or field and then use it:

Field protectedField = clazz.getDeclaredField("theProtectedField");
protectedField.setAccessible(true);
protectedField.get(obj); // Now you can access the variable. 

The same is about methods and constructors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top