java - Are static variables initialised from a file initialised only once -
i creating class in java represent string , color, called coloredstring. wanted able create 1 of these objects using color name of color recognized .net framework. such list of colors here: http://www.simplehtmlguide.com/colours.php. since java doesn't have comprehensive list of "known colors", solution have program read file populate list of known colors use. so, made text file following format:
aliceblue #f0f8ff antiquewhite #faebd7 aqua #00ffff aquamarine #7fffd4
the first word color name , second in format #rrggbb specify color code. since wouldn't want read file every time call constructor coloredstring, thought might have read static variable, in hopes when program runs, file read once , create single instance of static variable referenced instance of coloredstring. here code wrote this:
import java.awt.color; import java.io.file; import java.io.filenotfoundexception; import java.util.arraylist; import java.util.scanner; public class coloredstring { public static final arraylist<knowncolor> knowncolors = setkcs(); private static arraylist<knowncolor> setkcs() { try (scanner scanner = new scanner(new file("dotnet_knowncolors.txt"));) { arraylist<knowncolor> knowncolors = new arraylist<>(); while (scanner.hasnextline()) { string colorname = scanner.next().tolowercase(); string colorcode = scanner.next().touppercase(); knowncolors.add(new knowncolor(colorname, colorcode)); } return knowncolors; } catch (filenotfoundexception fnfex) { system.out.println("uh-oh..."); } return null; } public static final string defaultcolor = "black"; private color color; private string string; public coloredstring(string color, string string) { if (color == null || color.equals("")) { color = defaultcolor; } color = color.tolowercase(); this.string = string; if (color.startswith("#")) { this.color = new color(integer.parseint(color.substring(1), 16)); } else { this.color = getcolorbyname(color); } } public coloredstring(string string) { this.string = string; this.color = getcolorbyname(defaultcolor);//default color } private static color getcolorbyname(string name) { (knowncolor c : knowncolors) { if (c.name.equals(name)) { string colorcode = c.rgbhexnum; return new color(integer.parseint(colorcode.substring(1), 16)); } } return getcolorbyname(defaultcolor); } private static class knowncolor { public final string name; public final string rgbhexnum; public knowncolor(string name, string rgbhexnum) { this.name = name; this.rgbhexnum = rgbhexnum; } } }
so question this: code said , read file once, , create single instance of variable knowncolors, if create many instances of class?
check java first. below java 8; , used javafx.scene.paint.color
oracle sees javafx successor awt/swing.
it can 148 color names.
first test on colors:
string[] colornames = { "aquamarine", "aquamarine", "antiquewhite" }; // list color hex, alpha component ff (string colorname : colornames) { color color = color.valueof(colorname); system.out.printf("%s -> %s%n", colorname, color); }
then color constants:
(field field : color.class.getdeclaredfields()) { int modifiers = field.getmodifiers(); if (modifier.ispublic(modifiers) && modifier.isstatic(modifiers) && modifier.isfinal(modifiers) && field.gettype() == color.class) { string colorname = field.getname().tolowercase(locale.us); color color = color.valueof(colorname); system.out.printf("- %s -> %s%n", colorname, color); } }
this uses reflection, , assumes constant name human readable name.
[139] thistle -> 0xd8bfd8ff [140] tomato -> 0xff6347ff [141] turquoise -> 0x40e0d0ff [142] violet -> 0xee82eeff [143] wheat -> 0xf5deb3ff [144] white -> 0xffffffff [145] whitesmoke -> 0xf5f5f5ff [146] yellow -> 0xffff00ff [147] yellowgreen -> 0x9acd32ff
Comments
Post a Comment