• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

one more statement on constructor-static from K&B

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi pls clarify this statement also.

"you can access static variables and methods from constructors, although you can use them only as part of the call to super() or both()"
ex:super(classname.method());

means we cannot call static methods outside of super/this?

Thanks
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can access the static methods and variables in any where through the programme.
you can not access non static variables and method from the static context.
can you please explain your question a bit elaborately.

thanks®ards
krishna
scjp1.4
scwcd1.4
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class one
{
static int count =0;
public one()
{
saycount();
}
static void saycount()
{
count++;
System.out.println(count);
}
}

count's value 1.

When we are using static variables and methods there is no restriction that they should be used only with super keyword

If this is not what You are looking for pls revert back...

Folks,please correct me if im wrong
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by madhu v pe:
Hi pls clarify this statement also.

"you can access static variables and methods from constructors, although you can use them only as part of the call to super() or both()"
ex:super(classname.method());

means we cannot call static methods outside of super/this?

Thanks



Hi madhu,

In case of constructors we can access static variables and/or methods though using them only as part of the call to super() or both(). But we cannot access member variables/methods from constructors.

Static members can be accessed from anywhere in the class, be it outside or inside a super/this.

Hope i was able to clear your doubt.

Regards,
Gitesh
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gitesh R:
In case of constructors we can access static variables and/or methods though using them only as part of the call to super() or both(). But we cannot access member variables/methods from constructors.

Static members can be accessed from anywhere in the class, be it outside or inside a super/this.



I don't understand your explanation. Can you elaborate more, please?
To me, both statements contradict each other.

I also tried this code sample, where I try to access a static method and a static variable:



It compiles fine and gives the expected output.
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic