• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with protected variable...

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.. I am getting an error in the following code

CODE:-



As I am using protected within the same package.. the program should compile.. but I am getting an error:
B.java:19: cannot find symbol
symbol : variable x
location: class B
a.getInt(x);
^
1 error

Please help!!
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Milind Chaudhary wrote:Hi.. I am getting an error in the following code

CODE:-



As I am using protected within the same package.. the program should compile.. but I am getting an error:
B.java:19: cannot find symbol
symbol : variable x
location: class B
a.getInt(x);
^
1 error

Please help!!





the only way you can access the protected variable is through inheritance................the above vaiable cannot be visible...........
class A
{
protected int x=9;
int y;
void getInt(int j)
{
y=j;
System.out.print("y=" + y);
}
}

public class B extends A
{
public static void main(String[] args)
{
a.getInt(x);
}
}


now it should work fine try it.......................remember while using protected the only way you can access protected method and vaiable is through inheritance.......there is no other way out.............
 
Milind Chaudhary
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still getting the following error..

B.java:19: non-static variable x cannot be referenced from a static context
a.getInt(x);
^
1 error

Help!!
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the error is simply because the variable x is non static variable and we cannot access the non static variables or methods from static methods
you have to create the object of the class to access the variable
like this
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Milind Chaudhary wrote:I am still getting the following error..

B.java:19: non-static variable x cannot be referenced from a static context
a.getInt(x);
^
1 error

Help!!



many many sorry.........its right because the x variable is not a static............you cannot acces it from the main() method because main method is static one...........
so put the
a.getInt(x); above the main method.......................it will work......
otherwise make the variable(x) as static in A class.........
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


[EDIT]
the post was not so meaning ful
big mistake here
[/EDIT]
 
Milind Chaudhary
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well it seems protected is bit more confusing..

I got the following code to execute without any problems..



NOTE- Here I have not used inheritance as you guys suggested me.. but still it executed fine...
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what I wanted to say was that
you can access any variable in the same package
but in first case you were calling the protected variable without reference
that means
you were calling it as it was member of the class
in first case
you were calling it directly
i.e.
a.getInt(x);

the class B cannot access variables of class A without having reference of A
here x was getting accessed without the reference of variable a of type A

in your newest example
you are accessing it with the variable a
the class A belongs to the same package
hence we can access class A variables using the reference
you don't need to get confused about protected modifier here
when you write


//in above example it is accessing by reference


and in second case



hope you got it now
 
reply
    Bookmark Topic Watch Topic
  • New Topic