Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Access modifiers

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which statements are true?
int a .....(1)
static int a;........(2)
int f() {return a;}........(3)
static int f() {return a;}........(4)
(1)& (3) cannot occur in the same class definition
(2)& (4) cannot occur in the same class definition
(1)& (4) cannot occur in the same class definition
(2)& (3) cannot occur in the same class definition

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my dear friend the statement on the 3rd one is a true one.
ie a nonstatic variable can not be referenced by a staic metrhod in the same class. The other options are all possible. IF got doubt please try the following example in your comp. It will show your result.
This is a example which proves that 3rd statement is alone right.
/**************************************************************
public class example2 {
int a;
static int f() {
System.out.println(a);
return a;
}
public static void main(String args[]) {
example1 y1 = new example1();
y1.a = 20;
example1.f();
}
}
/*****************************************************************\
 
partha sarathy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my friend even try this the reverse it will workwhich makes your
4th statement is not true. Because we can have that declarations in the same class and also can access it.
here is the code . please try to access it and you acn get cleared
/************************************************************
public class example1 {
static int a;
int f() {
System.out.println(a);
return a;
}
public static void main(String args[]) {
example1 y1 = new example1();
example1.a = 20;
y1.f();
}
}
/****************************************************************
this will work for you. The other two are obvious we need not have to touth it as it is false
 
reply
    Bookmark Topic Watch Topic
  • New Topic