• 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Doubt: Main method

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

According to the rules that apply to the static and non static methods, they cannot access each other.
But then how is it tha we access non static( instance method) through the main method which is also static??

Does the JVM make an exception when it comes to the main method, allowing it to access the static as well as non static methods?

Thanks,
Piya
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

piya rai wrote:Hello,

According to the rules that apply to the static and non static methods, they cannot access each other.
But then how is it tha we access non static( instance method) through the main method which is also static??

Does the JVM make an exception when it comes to the main method, allowing it to access the static as well as non static methods?

Thanks,
Piya



Rule ? where is that speciified. One rule i can think of this context is that there is no "this"( current instance) inside static method. Yes certainly we can create a instance of classes and access inside static methods


Does the JVM make an exception when it comes to the main method, allowing it to access the static as well as non static methods?



Its possible in all static methods.
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See non-static members of the class can not the accessed from the static context as static members are for the class and instance members are for the objects/instances of the class and not the class itself. But the non-static members can refer to static members. There is no rule as such in this. Think about it, when you try to access a non-static member from a static method then how will the JVM understand that for which instance of the class are you referring to the instance member?? Hence you can not do so. Try to call a non-static method or use an instance variable from main() or any other static method...it will say non-static member can not be accessed from static context....

Hope this helps.
 
mansi gupte
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an excerpt from the K & B book, chapter Two , that raises my doubt.


A static method can't access a nonstatic (instance) variable, because there is no instance! That's not to say there aren't instances of the class alive on the heap, but rather that even if there are, the static method doesn't know anything about them. The same applies to instance methods; a static method can't directly invoke a nonstatic method. Think static = class, nonstatic = instance. Making the method called by the JVM (main()) a static method means the JVM doesn't have to create an instance of your class just to start running code.

The same applies for accessing nonstatic methods from a static method. The rule is, a static method of a class can't access a nonstatic (instance) method or variable o f its own class.
 
mansi gupte
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sudipto shekhar wrote:See non-static members of the class can not the accessed from the static context as static members are for the class and instance members are for the objects/instances of the class and not the class itself. But the non-static members can refer to static members. There is no rule as such in this. Think about it, when you try to access a non-static member from a static method then how will the JVM understand that for which instance of the class are you referring to the instance member?? Hence you can not do so. Try to call a non-static method or use an instance variable from main() or any other static method...it will say non-static member can not be accessed from static context....

Hope this helps.




Hello,
I agree to what you are saying.But the fact that we cannot access nonstatic methods from static{ main method in this case is not true] , because when i tried doing so the code compiled.




and that is the reason i asked if main has an exception.
 
Bartender
Posts: 2268
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did it really get compile?
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I agree to what you are saying.But the fact that we cannot access nonstatic methods from static{ main method in this case is not true] , because when i tried doing so the code compiled.



and that is the reason i asked if main has an exception.



I strongly disagree with you as the above code provided by you can not and will not compile.
 
mansi gupte
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sudipto shekhar wrote:

I agree to what you are saying.But the fact that we cannot access nonstatic methods from static{ main method in this case is not true] , because when i tried doing so the code compiled.



and that is the reason i asked if main has an exception.



I strongly disagree with you as the above code provided by you can not and will not compile.



I am sorry , the code din't compile , but it ran...
I wanted to say that the program runs fine.

========================================================================

can anyone specify why do i get the following error , when i try to do a garbage collection


1 error

C:\JAVA PROGRAMS\mock test questions>javac garbage.java
garbage.java:9: Runtime() has private access in java.lang.Runtime
Runtime rt = new Runtime();

Is it because the JVM doe not heed the request of garbage collection using gc() ??
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piya

Below mentioned are some of the code samples which can help you in understanding of K&B book lines

"A static method can't access a nonstatic (instance) variable, because there is no instance!"



Here , writer is trying to convey that instance variable (instance_var) can not be accessed without any object reference like we can access static variable (static_var)



"a static method can't directly invoke a nonstatic method."



Here , writer is trying to convey that instance method can not be accessed without object reference whereas static method can be accessed

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piya,
You can not call a non-static method from a static one.The only way to do so , is to use a dot(.) on the instance and c
all the non-static method.

How did you run the code , without compiling it?
 
swapnil dangore
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piya,
You can not call a non-static method from a static one.The only way to do so , is to use a dot(.) on the instance and c
all the non-static method.

How did you run the code , without compiling it?
 
Straws are for suckers. Now suck on this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic