• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic