File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

anyone could explain this?

 
M Mistroni
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
ind oing a mock exam i found this

# A class variable is declared using following modifiers

1. protected
2. private
3. public
4. static

i answered, 4 .. exam said correct answers were 1 and 4..

my assumption is that the mock examis injcorrect..

any comments?

regards
marco
 
Kashyap Hosdurga
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what you are asking?


class abc {
public int x=1;
private int y=2;
static int z=3;
protected int w=4;
public void gety() {
System.out.println("y="+y);
}
}
class xyz {
public static void main(String args[]) {
abc i = new abc();
System.out.println("x="+i.x);
i.gety();
System.out.println("z="+i.z);
System.out.println("w="+i.w);
}
}
[ April 20, 2006: Message edited by: Kashyap Hosdurga ]
 
Vishal Chawla
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankly,
it can be any combination of 1 & 4 , 2&4, 3 & 4, but 4 is the one that is required, so in short, i agree
 
bnkiran kumar
Ranch Hand
Posts: 176
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mistroni
A static variable can be public,private or protected there is no problem in that.

If you are asking about classes, outer class can be public or with default access specifier but inner classes can be public or protected or private or with default access specifier,static.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but static is not mandatory for inner classes, so i think question is wrong
 
Shaliey Gowtham
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But kiran static is required for the variable to be at class level
Inner classes doesn't allow the use of static, except for final primitives and Strings.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gowtham
i am telling like this,

class Kiran
{
static public class Gowtham
{
}
}
 
bnkiran kumar
Ranch Hand
Posts: 176
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which can also be
class Kiran
{
public class Gwotham
{
}
}
so i said static is not madatory, for static variables it is necessary.
 
Shaliey Gowtham
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even for the inner class you require a instance of an outer class to access so its not at class level.
But for top level nested class you can access it without an instance of the outer class.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic