Java Error Should Be Accessed In A Static Way
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site how to access a method in a static way in java About Us Learn more about Stack Overflow the company Business Learn more about java the static field should be accessed in a static way hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join
The Static Method Should Be Accessed In A Static Way Eclipse
the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why should the static
How To Access Static Method In C#
field be accessed in a static way? up vote 39 down vote favorite 6 public enum MyUnits { MILLSECONDS(1, "milliseconds"), SECONDS(2, "seconds"),MINUTES(3,"minutes"), HOURS(4, "hours"); private MyUnits(int quantity, String units) { this.quantity = quantity; this.units = units; } private int quantity; private String units; public String toString() { return (quantity + " " + units); } public static void main(String[] args) { for (MyUnits m : how to access static method from another class in java MyUnits.values()) { System.out.println(m.MILLSECONDS); System.out.println(m.SECONDS); System.out.println(m.MINUTES); System.out.println(m.HOURS); } } } This is referring to post ..wasnt able to reply or comment to any so created a new one. Why are my System.out.println(m.MILLSECONDS); giving warnings-The static field MyUnits.MILLSECONDS should be accessed in a static way ? Thanks. java static share|improve this question edited Apr 12 '11 at 23:53 user unknown 20.7k74290 asked Apr 12 '11 at 23:35 Ava 1,636154175 5 I changed the title, which was "Java Code Optimization". Please take more care about the question title. –user unknown Apr 12 '11 at 23:48 ya my bad, will take care from next time :) –Ava Apr 12 '11 at 23:56 add a comment| 3 Answers 3 active oldest votes up vote 74 down vote accepted Because when you access a static field, you should do so on the class (or in this case the enum). As in MyUnits.MILLISECONDS; Not on an instance as in m.MILLISECONDS; Edit To address the question of why: In Java, when you declare something as static, you are saying that it is a member of the class, not the object (hence why there is only one). Therefore it
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta
How To Access Static Method Of A Class In Java
Discuss the workings and policies of this site About Us Learn more how to access static variable in java about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack call static method java Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, http://stackoverflow.com/questions/5642834/why-should-the-static-field-be-accessed-in-a-static-way helping each other. Join them; it only takes a minute: Sign up What is the proper way of accessing static fields in Java? up vote 0 down vote favorite 1 I just started learning Java and I wrote a class to test using static fields. Everything works fine but in Eclipse I see an icon which when hovered http://stackoverflow.com/questions/5562321/what-is-the-proper-way-of-accessing-static-fields-in-java comes out as: "The static method getCounter from the type CarCounter should be accessed in a static way." What's the right way then? Here's the class: public class CarCounter { static int counter = 0; public CarCounter(){ counter++; } public static int getCounter(){ return counter; } } And here's where I try to access variable counter: public class CarCounterTest { public static void main( String args[] ){ CarCounter a = new CarCounter(); System.out.println(a.getCounter()); //This is where the icon is marked } } java class variables static share|improve this question edited Dec 8 '11 at 3:28 MPelletier 10.3k106296 asked Apr 6 '11 at 6:35 enchance 6,225195085 add a comment| 6 Answers 6 active oldest votes up vote 5 down vote accepted Static fields and methods are not belong to a specific object, but to a class, so you should access them from the class, and not from an object: CarCounter.getCounter() and not a.getCounter() share|improve this answer edited Apr 6 '11 at 6:49 answered Apr 6 '11 at 6:38 MByD 98.1k17184214
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers http://stackoverflow.com/questions/13537443/java-accessing-static-method-properly or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question http://stackoverflow.com/questions/18214493/the-static-field-should-be-accessed-in-a-static-way x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up JAVA : Accessing static method properly up vote 6 down vote favorite 2 I am new to JAVA, and I like to try and understand everything. When accessing a static method "hero.returnHp()" in how to JAVA, I have the following: hero Mike = new hero(); Mike.returnHp(); The program runs fine, but I notice that Eclipse has a warning stating, "The static method from the type hero should be accessed in a static way." When I accept the auto-fix, it changes "Mike.returnHp();" to "hero.returnHp();". So I have two questions: 1) What is the advantage of this? 2) If I created two objects of the same type, how would I specify which one to return when accessing how to access in a static way? Thanks! java object static share|improve this question edited Nov 24 '12 at 1:27 steveax 11.3k42450 asked Nov 24 '12 at 1:21 Hermes Trismegistus 155228 2 The point of static methods is that they are not tied to instances of the class. So if you call one with an instance on the left of the dot, or the class name, doesn't make a difference. –Daniel Fischer Nov 24 '12 at 1:24 4 Well, one problem is that you're using leading caps for variable names and leading lower case for classes -- exactly opposite the accepted standard. But, IIRC, either form you use above is technically legal, though the "preferred" form is to use the class name when calling a static. –Hot Licks Nov 24 '12 at 1:31 "When I accept the auto-fix, it changes Mike.returnHp(); to hero.returnHp(); Mmmm rather the other way, I guess. –leonbloy Nov 24 '12 at 1:32 @leonbloy No, since it's static, the fix is to call it with the class name. The class name is lower case here, that might have misled you. –Daniel Fischer Nov 24 '12 at 1:33 1 Offtopic: As @HotLicks mentions, your way of using upper and lowercase is the oposite of what the Java community uses and it will tend to confuse people. I strongly advise you to learn the common way now that you are starting. bitprison.net/j
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up The static field should be accessed in a static way up vote 0 down vote favorite 1 I have different Exception category Enum as below public enum GSBBCacheCategory { SEARCH(9001), UPDATE_PERSECURITY(9002), CROSS_REFERENCING_PERSECURITY(9003), METADATA_SEARCH(9004), REMOVEALL(9005), UPDATE_BACKOFFICE(9002); private int exceptionCode; GSBBCacheCategory(int exceptionCode) { this.exceptionCode = exceptionCode; } public int getExceptionCode() { return exceptionCode; } } public enum GSBBEncryptionCategory { . . . } I want to provide one place to access these Enum in client code. Presently I achieved this as below public class GSBBExceptionCodes { public static GSBBDecryptionCategory decryptionCategory; public static GSBBCacheCategory cacheCategory; } Now to access exception code I have do something like below public static void main(String[] args) { System.out.println(GSBBExceptionCodes.decryptionCategory.ERRORCODE_DECRYPTION_FAILURE); System.out.println(GSBBExceptionCodes.cacheCategory.UPDATE_PERSECURITY); } Which says “The static field GSBBDecryptionCategory.ERRORCODE_DECRYPTION_FAILURE should be accessed in a static way” Is it possible to achieve above without any warning? java static enums share|improve this question asked Aug 13 '13 at 16:33 Vipul 397620 add a comment| 3 Answers 3 active oldest votes up vote 5 down vote There are two ways to reference a static member (either a field or a method). One is WhateverClass.the