• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

public field

 
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the K&B Software test

Which of these are true?
A public field can be accessed and modified from anywhere.



I said it was true (which the test says is right) but what about the following:


Clearly the public field x in Class1 is not accessible to the m1() method in Class2. Is a field not considered public if its class isn't public even if it is marked public? I just want to make sure I understand the precise definition of a public field. Thanks!
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kendall Ponder wrote:Clearly the public field x in Class1 is not accessible to the m1() method in Class2. Is a field not considered public if its class isn't public even if it is marked public? I just want to make sure I understand the precise definition of a public field. Thanks!


Your code doesn't compile! Class1 needs to be public in order to be accessible in another package (and you must use an import statement or the fully qualified name for Class1 as well).
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Kendall Ponder wrote:Clearly the public field x in Class1 is not accessible to the m1() method in Class2. Is a field not considered public if its class isn't public even if it is marked public? I just want to make sure I understand the precise definition of a public field. Thanks!


Your code doesn't compile! Class1 needs to be public in order to be accessible in another package (and you must use an import statement or the fully qualified name for Class1 as well).



I know it doesn't compile. It doesn't compile because the x field in class one isn't accessible even though it is a public field. My point is the statement "A public field can be accessed and modified from anywhere" is false, but the test answer is true.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kendall Ponder wrote:I know it doesn't compile. It doesn't compile because the x field in class one isn't accessible even though it is a public field.


No, your code doesn't compile, because Class1 isn't visible. Has nothing to do with the x field. You don't get a compiler error on line int y =a.x; but on the import statement and the line Class1 a=new Class1();.
 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First the class the variable is in has to be visible. If it isn't it doesn't matter what the visibility of the variable is. If the class is visible then next you check the variable visibility. IOW class visibility trumps variable/method visibility.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if the question would be testing you about the default access level (of a class), it would mention it specifically in the question. Like:
  • A public field in a class without access modifier can be accessed and modified from anywhere
  • A public field in a package-private class can be accessed and modified from anywhere
  • A public field in a class with default access can be accessed and modified from anywhere
  •  
    Kendall Ponder
    Ranch Hand
    Posts: 205
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:And if the question would be testing you about the default access level (of a class), it would mention it specifically in the question. Like:

  • A public field in a class without access modifier can be accessed and modified from anywhere
  • A public field in a package-private class can be accessed and modified from anywhere
  • A public field in a class with default access can be accessed and modified from anywhere


  • This is why they teach us in the education field true/false questions are bad. The statement as written is false (which your sample questions prove) so I have to assume they are talking about a public field in a public class but that is not what the question said. The vast majority of the questions on the practice exams don't have this issue and Oracle may have some questions like this so it is probably good to have it included. But if I had a question like this on a test I gave to my high school physics class and the students pointed out the problem I would throw the problem out.
     
    Kendall Ponder
    Ranch Hand
    Posts: 205
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:

    Kendall Ponder wrote:I know it doesn't compile. It doesn't compile because the x field in class one isn't accessible even though it is a public field.


    No, your code doesn't compile, because Class1 isn't visible. Has nothing to do with the x field. You don't get a compiler error on line int y =a.x; but on the import statement and the line Class1 a=new Class1();.



    Sorry I left the import statement out. I guess I shouldn't have put in the code it has distracted from my point. The bottom line is a public field in a default class is not accessible outside of its package so the statement "A public field can be accessed and modified from anywhere." is false, but the sample test says it is true. The statement "A public field in a public class can be accessed and modified from anywhere." would be true but that is not what it said.
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Kendall Ponder wrote:The bottom line is a public field in a default class is not accessible outside of its package


    But that has nothing to do with the access modifier of the variable! It's due to the access modifier of the class. We all know that public is the whole wide world!

    Kendall Ponder wrote:The statement "A public field in a public class can be accessed and modified from anywhere." would be true but that is not what it said.


    This would incline a complete rewrite of the whole book. Because just stating public field, protected method,... is not enough anymore. You always have to mention the access level of the class and if it's default access if the other class is in the same package or not. That would be a very cumbersome study guide in my humble opinion. It's much simpler to do the opposite as I stated in my previous post: if it is about a specific situation (public field in a package-private class) clearly state this in the question and/or answers.

    We all know constants defined in an interface are inherently public and thus accessible in the whole wide world. This wouldn't be true anymore for the same reason.
    But on the other hand, I can also show that you can still access a constant defined in a package-private interface outside its package. How? With a little bit of imagination and class abuse
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic