Win a copy of Fixing your Scrum this week in the Agile 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

question about protected and static

 
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Imagine that we have two clases on two separate files.




at first sight what do you thing happend? and why?

thank you
[ February 20, 2008: Message edited by: Milton Ochoa ]
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, the import statement seems out of place. import statements belong at the beinning of the source file after the optional package statement.

You could separate the code into two class files or consolidate the code into a single class in one class file.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At first site,i think this program will run successfully and will print "8" on the console. This is simple concept of static and protected, as outside the package,protected will be available to subclasses only and your test class is subclass of pack so now protected will be available. now two condition can occur:
if
1) protected variable is instance
2) protected variable is static

1) In first case, you need to construct an Object first then only you can refer to that variable.

2) As now it is static so you can refer by className.variableName
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And yes while replying to this question I have assumed that both test and pack are different.I mean to say we have two source file pack.java and test.java.

If not then your program will not compile because pack is public and and test is default and you have defined main in test( compile time error ) and also improper place of import statement (compile time error)
[ February 19, 2008: Message edited by: Sunny Jain ]
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic