• 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

anyone could explain this?

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but static is not mandatory for inner classes, so i think question is wrong
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gowtham
i am telling like this,

class Kiran
{
static public class Gowtham
{
}
}
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic