Paul's suggestion of the Timer seems like what you're asking for. But instead of "constantly checking" to see if the variable has changed, you might consider making the variable private, with the only way to change it through a setter method. Then whenever the variable is changed, that setter method could also send notifications. (Paul: I think "constant" is used here ... |
Personally I prefer the first option, because it more or less follows the concept of variable -> operator -> constant. The second option has the 'benefit' that if s is null, it will return false, instead of throwing a NullPointerException. I actually like that it throws a NullPointerException, because it indicates that something in my program might have gone wrong. Others ... |
|
how to give multiple parameter to constant in enum ? in this way it gives error C:\Users\Admin\Documents\java source code\Niit\testdrive\enumurated class>javac TestGadget.java TestGadget.java:3: ')' expected NETBOOK(" Netbook" " Sony" ), ^ TestGadget.java:3: ',', '}', or ';' expected NETBOOK(" Netbook" " Sony" ), ^ TestGadget.java:3: '}' expected NETBOOK(" Netbook" " Sony" ), ^ TestGadget.java:9: class, interface, or enum expected private final String name; ... |
|
public String toString() { String info = ""; if( getOn() == true) { info = "The fan speed is: " +getSpeed() +", the radius is: " +getRadius() +", the color is: " + getColor(); } else { info = "The color is: " +getColor() + ", the radius is: " +getRadius() +", the fan is OFF."; } return info; } |
|
|
The idiom is "public static final" for constants and "private static final" for private constants. The other common idioms are singletons and enums. There is no sense in creating instance variables for constant values, I don't know how clever the compiler is with final instance variables, but it probably wastes a bit of memory. |
System.out.println ("Enter the name of the employee."); employeeName = scan.nextLine(); System.out.println ("Is this employee part time or full time? parttime or fulltime"); employeeType = scan.next(); if (employeeType.equals("fulltime")) { System.out.println ("Please enter the number of hours worked, not including overtime."); workedHours = scan.nextDouble(); System.out.println ("Please enter the number of overtime hours worked this pay period."); overtimeHours = scan.nextDouble(); System.out.println ("Enter the salary ... |
In addition, if the expression is a constant expression (?15.28) of type byte, short, char or int : * A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable. * A narrowing primitive conversion followed by ...
|
Hey, I'm trying to understand constant fields in Calendar class. What does it mean that the value for the field for exapmle DAY_OF_YEAR is equal 6. Why the values are not unique.How can I apply set() and add() methodds to this fields althow they are marked static final. Thank u for responds, Nina |
|
Good choice evil. Just one suggestion... I see lots of constants which I believe would be better stored in property files.... it may seem like overkill and just sooooo cumbersome to start with, but once you've built up a decent "properties toolkit" it's quick, easy, reliable, portable, I18/L10N'able, and just simply FLEXIBLE. Test cases are the only place I allow myself ... |
|
Lately, it seems like a lot of people are receiving really bad basic Java instruction. Maybe it's just me, but it seems like there's been a jump recently in questions like this one, or "What's better, String or StringBuffer?" as if they were completely interchangeable (in fact, over at JavaRanch, that exact question seems to be posted about once a week.) ... |
I'm trying to write a fairly straight forward Genetic Algorithm, part of this involves the use of ephemeral random constants. Essentially when a new individual of the population is created it can choose a random value as a constant, however this then stays fixed for that individual. I couldn't find anything on the forums about this, but then it might be ... |
Well.. I have been new to Java myself too.. And I probaly also asked these stupid kind of questions too, and I HATED when I got responses telling me to read The Java Tutorials over and over again.. -.-' So I'm just going to give you the answer: B =) and now not to get hated by the community I'm going ... |
the command "jconsole" open a empthy J2SE 5.0 Monitoring & Management Console C:\Dokumente und Einstellungen\Frei.SATAGARBON\desktop\java>jconsole Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: java/nio/ DirectLongBufferU at java.nio.DirectByteBuffer.asLongBuffer(DirectByteBuffer.java:774) at sun.management.counter.perf.PerfDataEntry.longData(PerfDataEntry.java :190) at sun.management.counter.perf.PerfInstrumentation.getNextCounter(PerfIn strumentation.java:110) at sun.management.counter.perf.PerfInstrumentation.findByPattern(PerfIns trumentation.java:144) at sun.management.ConnectorAddressLink.importFrom(ConnectorAddressLink.j ava:70) at sun.tools.jconsole.ConnectDialog$ManagedVmTableModel.getManagedVirtua lMachines(ConnectDialog.java:539) at sun.tools.jconsole.ConnectDialog$ManagedVmTableModel.refresh(ConnectD ialog.java:511) at sun.tools.jconsole.ConnectDialog$ManagedVmTableModel.(ConnectDi alog.java:502) at sun.tools.jconsole.ConnectDialog.(ConnectDialog.java:139) at sun.tools.jconsole.JConsole.showConnectDialog(JConsole.java:571) at sun.tools.jconsole.JConsole.access$100(JConsole.java:34) at sun.tools.jconsole.JConsole$4.run(JConsole.java:702) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:461) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh read.java:242) at ... |
Should I code constants like an IOException into a constant class? There are two approaches I would like to use but am unsure which to pick. 1) I create a common application constants class that declares for eg. an IOException message that is used by various classes. This class allows this message to be changed here for purposes of i8n. 2) ... |
|
|
|
If you are serious about being a java developer, my favorite book of all the java books I have (about 20 or so) is the java pocket guide by oreily. When I forget something like this then I can turn to it real fast and get my answres without a bunch of fluff information. You can pick it up for 15 ... |
|
|
|
But then, it always gives me the different result. Sometimes it was not even close. The time complexity of the firstAttempt() is n^2, and nlogn for secondAttempt(), and n for thirdAttempt(). But sometimes the secondAttempt() whose execution time is supposed to be faster than the firstAttempt() takes more time, and sometimes the it's as fast as 0ms. Why does it always ... |
|
Hi! I am trying to figure out what would be a suitable way of implementing a set of default constants that should be possible to extend with custom ones. I am creating an API that will have several sets of constant values but the user of the API should be able to extend/register/add additional constants to these sets. In my first ... |
30. Constants forums.oracle.comtnx both of you, and please dont fight about this thread. My apologies to you flounder if you find my question vague enough. Tnx ejp for your easy understanding of my question. By the way, this is the set-up of my code. if have my package, let's call it mypackage. mypackage contains three class MyPackageConstants.class // contains all the constants (static ... |
|
32. Constants forums.oracle.comHopefully you are aware that compile-time constants are taken by the value at compile time. If you ever decompiled an AWT program, you could see a bunch of arbitrary integers and not symbolic constant references. Therefore if in a complied class you find a constant "A", you can not know whether it came from Constants.A or was hard-coded or came from ... |
|
I can't figure out why this is not working ... i.e. String label = BUTTON_WORK_ON; (which is an constant from an interface, already implements this interface) XXX.getLabel(BUTTON_WORK_ON) // it returns the right label i want ... it doesn't work if i do this. i like to pass the label string instead, cos' it is a variable ... XXX.getLabel(label) => // nothing ... |
String str = "Hello " + "World"; If I write like the above, how many string objects are created? One or Three? I am asking this because somewhere I found an advice that we should use a StringBuffer and its append() method to concatenate strings in order to reduce the number of string constants generated when we want to create a ... |
It's your answer that begs the question, not me. Anyway, this is what Sun says regarding the use of EnumMap: +EnumMap, which is internally implemented as an array, is a high-performance Map implementation for use with enum keys. This implementation combines the richness and safety of the Map interface with a speed approaching that of an array.+ If you want to ... |
The problem is that the brittleness is inevitable if you're going to get the advantages of compile time constants. Compile time constants are compiled into the classes that use them. Associate them with enums and you're looking them up at runtime (and you won't be able to use them, e.g. in case statements). |
Hi everyone, I'm working on a lab for a computer science class I'm taking. I have to create a singly-linked list with a head and a tail, and include methods for inserting and removing nodes from the front and the end of the list, all in constant time. My problem is that I cannot figure out how to remove a node ... |
What to you think about overusing string constants in the code instead of "bla bla bla" strings often repeatedly thought the code. Why am I asking? Not far ago I have taken this behavior from my TL. He is prefer to use string constants, he brings to the constants most of the strings literals from the code. I think this way ... |
|
Polymorphism is a property of methods, not fields. The fact that A and B both have a field with the name TEST is a coincidence, nothing more. To access the values in a polymorphic fashion, you will have to use an overridden method, or reflection. I recommend using the method as you have been doing. Why aren't your constants static, by ... |
I'm looking for some advice on the best practice for bundling application scoped constants. I've seen such things as putting the constant definitions in an interface, which is then imported in each class that needs to reference those constants. If anyone has any comments, or a better idea, please let me know. |
|
Write a program with two constant values, one with alternating binary ones and zeroes, with a zero in the least-significant digit, and the second, also alternating, with a one in the least-significant digit (hint: It's easiest to use hexadecimal constatnts for this). Take these two values and combine them in all possible ways using the bitwise operators, and display the results ... |
|
|
Suppose I define a class to contain a set of constants whose values ar stored in a database. Is there a way for the class to create constants based upon values retrieved from a database? In other words suppose I have a database table that stores information about shapes like this: SHAPES_TABLE { SHAPE_ID INT SHAPE_DESC VARCHAR } Now the table ... |
|
|
Hi, well the topic says it. int x = MyEnum.SOME_VALUE.ordinal(); // x stores the index MyEnum e = ??? // revert back to enum using index x How the heck do I do that? I'm kind of new to java and generics, but valueOf doesn't seem to help me, I might be wrong though. Thanks in advance |
|
COMMA(","); ..... String address = name + COMMA + surname + COMMA; wouldnt it be better to hardcode it on the basis that if the value of the constant ever changed then it probably wouldnt suit my purpose and I would have to refactor the code where it was being used anyway , ie: String address = name + "," + ... |
Iin the UTF-8 charset, between (19968 < character < 40846) are 1945 characters that i am interested in. They are now in a text file. My only purpose is to test if a character is inside this Set. my design: (1) use an unmodifiable Set. (2) make it a singleton. __question__ Is it a better design to have a class/interface with ... |
quote: The length of field and method names, field and method descriptors, and other constant string values is limited to 65535 characters by the 16-bit unsigned length item of the CONSTANT_Utf8_info structure (4.4.7). ----JVMS but, for the declaration below: String s = "aaaaa....aaaaa"; If the length of String literal reach 65535, the compiler will generate a compile error, if 65534, no ... |