tag:blogger.com,1999:blog-8712770457197348465.post1201832724389142337..comments2024-03-17T23:01:26.872-07:00Comments on Javarevisited: What is Static Variable Class method and keyword in Java - Example Tutorialjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger25125tag:blogger.com,1999:blog-8712770457197348465.post-38863019522565074542016-02-06T04:23:57.470-08:002016-02-06T04:23:57.470-08:00@raj, yes you can use volatile with static, it'...@raj, yes you can use volatile with static, it's legal in Java but be careful static variables often cause thread-safety errors in Java program.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21992643765229900342016-02-06T02:54:00.009-08:002016-02-06T02:54:00.009-08:00Can we use volatile with static variable?Can we use volatile with static variable?rajhttps://www.blogger.com/profile/10808672024638303367noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-9071830826907504552015-09-24T03:49:28.983-07:002015-09-24T03:49:28.983-07:00Hi all
I wana ask that can we use static variable ...Hi all<br />I wana ask that can we use static variable in static method, which already have a local static variable in it's method definition Amar Bagalnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83697832259441808332015-04-29T09:01:24.460-07:002015-04-29T09:01:24.460-07:00It is true that the memory for class (static) vari...It is true that the memory for class (static) variables declared in the class is taken from the method area.<br />But according to https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5.4<br />Although the method area is logically part of the heap.Anshudeephttp://netjs.blogspot.comnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-3689346354883329692015-04-27T22:53:54.343-07:002015-04-27T22:53:54.343-07:00will static variables consume more memory in heap ...will static variables consume more memory in heap while using in java than non-static variables.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-63893721287100243342014-12-18T05:47:13.274-08:002014-12-18T05:47:13.274-08:00Definitely in most of the programming languages st...Definitely in most of the programming languages static variables are stored in heap memory only.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54287575766044877612014-11-26T02:57:23.804-08:002014-11-26T02:57:23.804-08:009. If you try to override a static method with a n...9. If you try to override a static method with a non-static method in sub class you will get compilation error<br /><br />same is true the other way round i.e.<br />9. If you try to override a NON-static method with a static method in sub class you will get compilation error<br />Kunal Krishna https://www.blogger.com/profile/06202237214255825462noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-47688955917114349152014-11-26T02:39:10.272-08:002014-11-26T02:39:10.272-08:00...concept of method overloading doesn't apply......concept of method overloading doesn't apply to static methods......<br />VERY wrong:<br />Can we overload static methods? YES<br /><br />public class Test {<br /> public static void foo() {<br /> System.out.println("Test.foo() called ");<br /> }<br /> public static void foo(int a) { <br /> System.out.println("Test.foo(int) called ");<br /> }<brKunal Krishna https://www.blogger.com/profile/06202237214255825462noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-9246240545742993712014-11-26T02:38:15.761-08:002014-11-26T02:38:15.761-08:00typo error :
4) you can not use non-static variab...typo error :<br /><br />4) you can not use non-static variable inside a static method , it will result in compilation error as shown below. See ""Why static variable can not be called from static method for more details."" <br />-->> See "Why NON-static variable can not be called from static method for more details"<br />Thankx<br />Kunal Krishna https://www.blogger.com/profile/06202237214255825462noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14879104635472295642014-10-14T03:27:34.362-07:002014-10-14T03:27:34.362-07:00Hi.. I got an issue with the static block.
Hi.. I got an issue with the static block.<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-39224324861721550752014-04-25T00:01:52.626-07:002014-04-25T00:01:52.626-07:00HI javin, I find it very informative regarding you...HI javin, I find it very informative regarding your blog, and I have a doubt regarding the below statement in the above blog.<br /><br />"In other words there is only one copy of static variable will be present in Java Heap memory, which can be accessed or altered by any object."<br /><br />But when i am learning java, i believe that static variables will get stored in method area not Naganoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-51241690710511375682014-03-04T13:41:46.668-08:002014-03-04T13:41:46.668-08:00Hi,
I think Mani pointed this typo regarding point...Hi,<br />I think Mani pointed this typo regarding point 4,<br />which says "Why static variable can not be called from static method for more details."<br />and the link says: "Why non-static variable cannot be referenced from a static context?"<br />Thanks, AttAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54528575155748436732013-11-19T08:36:16.966-08:002013-11-19T08:36:16.966-08:00regarding the Static keyword code please look at ...regarding the Static keyword code please look at the following <br /> public class TradingSystem {<br /><br /> private static String category = "electronic trading system";<br /> public static void main(String[] args) {<br /> TradingSystem system = null;<br /> System.out.println(system.category);<br /> }<br /><br />I just wanted to share that during runtime Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-17929741318119591462013-09-21T00:26:35.436-07:002013-09-21T00:26:35.436-07:00Hi Mani,
Point 4 is absolutely correct.
Please wr...Hi Mani,<br /><br />Point 4 is absolutely correct.<br />Please write a program and u will see it yourself.<br /><br />Thanks<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36142265888555324752013-07-01T06:02:32.843-07:002013-07-01T06:02:32.843-07:00Hi,
Here a small correction needs to be done at t...Hi,<br /><br />Here a small correction needs to be done at the 4th point in this page.<br /><br />Non-static variables cant be called from a static method. I was confused by this, that is why i wanted to point this.<br /><br />URL:<br />http://javarevisited.blogspot.com/2011/11/static-keyword-method-variable-java.html<br /><br />Thanks,<br />ManiAnonymoushttps://www.blogger.com/profile/12799656176382241812noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-10853920063287591572013-05-16T00:11:39.648-07:002013-05-16T00:11:39.648-07:00class A
{
static int a = 30;
void msg()
{
a= 2...class A<br />{<br /><br />static int a = 30;<br /><br />void msg()<br />{<br /> a= 20;// here by default fach int value why?<br />System.out.println(a);<br />}<br />public static void main(String []args)<br />{<br /><br /><br />A c = new A();<br />c.msg();<br /><br />}<br />}pavan kumarhttps://www.blogger.com/profile/11000030252739882374noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-57453578690347093722012-04-11T22:14:11.716-07:002012-04-11T22:14:11.716-07:00Can we override static method in Java?
When to us...Can we override static method in Java? <br />When to use static method in Java?<br />What is the benefit of using Static method in Java?<br />How to write unit test for testing static methodAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-44957661294963874362012-03-21T02:20:16.860-07:002012-03-21T02:20:16.860-07:00my question is what does the keyword static mean i...my question is what does the keyword static mean in Java ? I see we can use static keyword with fields, methods, nested class, initialize block etc but what does static mean, does static mean something which is not dynamic or same for all ?sujatanoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13781434751957998402012-02-16T21:51:06.615-08:002012-02-16T21:51:06.615-08:00This post was really helpful to understand about s...This post was really helpful to understand about static keyword in java..<br />thnks for u and keep provide this kind of post..Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32211291961415199282011-12-05T00:20:31.592-08:002011-12-05T00:20:31.592-08:00@Rashmi, Difference between static nested class an...@Rashmi, Difference between static nested class and non static nested class (inner) class is that static doesn't associated with any outer class instance and its instance can be created even before creating any instance of outer class while in case of inner class you first need to create outer class instance and than inner class.static keyword can only be applied to nested class and not on Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33689273823570773982011-11-19T21:53:19.207-08:002011-11-19T21:53:19.207-08:00Can we make a Class static in Java, What are diffe...Can we make a Class static in Java, What are differences between a static class and non static class in Java, does static class load faster than non static class in Java ?Rashminoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-5924404965273174162011-11-08T02:15:28.351-08:002011-11-08T02:15:28.351-08:00Thanks for your Comment Peter, That's indeed a...Thanks for your Comment Peter, That's indeed a clever way to define Singleton.Javin @ Date To String in Javahttp://javarevisited.blogspot.com/2011/09/convert-date-to-string-simpledateformat.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55371496793938935402011-11-08T00:31:45.769-08:002011-11-08T00:31:45.769-08:00I would add a note that static blocks are implicit...I would add a note that static blocks are implicitly thread safe. There is no issue with multiple threads trying to load the same class at once. This also means the simplest thread safe, lazy loading singleton is just<br /><br />enum Singleton {<br /> INSTANCE;<br />}Peter Lawreyhttps://www.blogger.com/profile/17982030676088168612noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-62284880006667973092011-11-07T22:17:44.592-08:002011-11-07T22:17:44.592-08:00You mean to say the static fields will be unloaded...You mean to say the static fields will be unloaded once the class object is garbage collected?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-77057314527660911952011-11-07T20:22:09.470-08:002011-11-07T20:22:09.470-08:00One important point you may want to add is loading...One important point you may want to add is loading and unloading of static fields. Static fields or variables are initialized when Class is first loaded by ClassLoader while they are unloaded from memory if there is no live reference of field from any Thread static is present and they are eligible for Garbage Collection.Anonymousnoreply@blogger.com