• 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

Public Variables

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<CODE><PRE>class A {
int a=1;
int b=2;
public int c=6;
public int d=3;
private int e=8;
private int f=8;
protected int g=10;
protected int h=11;
}
class B {
int a1=c;
// int a2 =g;

/* public static void main(String[] args) {
System.out.println(a);
} */
}</CODE></PRE>
Compilation error:
B.java:15: Undefined variable: c
int a1=c;
Is it not possible to acces a public variable.
But as far as i know its said that public can be accessed inside or outside the class.Then,
Why iam getting a compilation errror.
Any suggestions or comments?
 
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are these classes in two different files?
I can't see the closing brace for B.
There is no c defined within B and B is not an inner class of A, so it makes sense. You will need an instance of A and use that to get to c.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there'a fairly major misunderstanding going on here. To simplify your question a little, you say:
<pre>
class A
{
public int c = 6;
}
class B
{
int a1 = c;
}
</pre>
This still won't compile, but all the non-essential bits have been removed.
Now. In Java, everything belongs to a class. There are no "global variables" or functions which exist on their own. You seem to have understood this, which is good. However, there are two ways for something such as a variable to belong to a class. It can be either part of an object created from the class, or part of the class itself. Everything is assumed to be part of an object, unless the "static" keyword is used to indicate that it is part of the class.
Here's an example:
<pre>
class A
{
public static int x; // only one of it, part of the class
public int y; // each object has a different copy
public void show()
{
System.out.println("x is " + x + ", y is " + y);
}
}
</pre>
To access something which is part of an object, you need an object, and you need to say which object you are referring to. The easiest way to get an object is to call "new":
<pre>
A a = new A();
</pre>
Once you have such an object, you can access any public variables and methods, for example:
<pre>
int result = a.y;
a.show();
</pre>
Anything which is declared "static" can also be accessed through the class name:
<pre>
int another = A.x;
</pre>
So lets put all this together into a real compilable file. Cut and paste the following to see it work:
<pre>
class A
{
public static int shared = 12;
public int different = 13;
public void show(String name)
{
System.out.println(name + ": shared is " + shared + ", different is " + different);
}
}
public class B
{
public static void main(String[] args)
{
A a1 = new A();
A a2 = new A();

System.out.println("initial values");
a1.show("a1");
a2.show("a2");
System.out.println("change one object");
a1.different = 26;
a1.show("a1");
a2.show("a2");
System.out.println("change the shared value");
A.shared = 99;
a1.show("a1");
a2.show("a2");
System.out.println("change it a different way");
a2.shared = 75;
a1.show("a1");
a2.show("a2");
}
}</pre>
To summarise. In your example you are trying to access a variable which is part of an object, but you don't say which object to look at - which is why it won't compile. The choices you have to make it work are (a) mark it as "static" and use "A.c" to access it, or (b) create an object of class A and use that to access it.
I hope this has helped.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For accessing the members of class A you must have
1. A instance of class A
2. U must extends class A
If any one of this is implemented you can access the public variables.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you missed the question with the other reply's. They were asking about INNER classes. If you move the braces to make C an INNER class to A (shown below) it compiles and runs fine.

Originally posted by Roll:
<CODE><PRE>class A {
int a=1;
int b=2;
public int c=6;
public int d=3;
private int e=8;
private int f=8;
protected int g=10;
protected int h=11;
// this is your problem... class B is NOT an inner class of A
// } So I commented it out
class B {
int a1=c;
// int a2 =g;
// add the close of B then the close of A
}
// and close A
}
// get rid of this your referencing a non static variable from a static method... but that is another story
/*public static void main(String[] args) {
System.out.println(a);
} */
</CODE></PRE>
Compilation error:
B.java:15: Undefined variable: c
int a1=c;
Is it not possible to acces a public variable.
But as far as i know its said that public can be accessed inside or outside the class.Then,
Why iam getting a compilation errror.
Any suggestions or comments?



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic