Java programming FAQAnswers to frequently asked questions about Java programming.How do I call another class in the same folder- (Found June 24, 2008 ) Assuming the Java classes are in the same package, one class should instantiate the other to call an instance method, or use a class name reference to call a static method, as below.http://www.codestyle.org/java/FAQ.shtml#othermethodcall How do I get the domain name from a URL- (Found June 24, 2008 ) A: If you want to extract a domain name from an existing URL object, use the URL's getHost() method.http://www.codestyle.org/java/FAQ.shtml#domainfromurl How do I get a domain name from an IP address- (Found June 24, 2008 ) A: It is possible to do a reverse name look-up for an IP address using the java.net.InetAddress class. The example below first gets an instance of the class using the static getByName(String) method, then applies the getHostName() method to it. The method will only resolve a domain name if there is one configured for the host, otherwise the method returns the original IP address.http://www.codestyle.org/java/FAQ.shtml#domainresolution My Java Web client gets the wrong site!- (Found June 24, 2008 ) A: Many Web sites use virtual hosting, which requires you send an HTTP Host header with your request.http://www.codestyle.org/java/FAQ.shtml#hostheader How can I set a code name identifier for my feed reader- (Found June 24, 2008 ) Your application should pass a User-Agent header in the HTTP request with the code name of your application as the header value. Web browser user agents often include the full version and build number for the application and the software platform it was built for. Feed readers tend to use simpler name and version number combination. See the Java example below.http://www.codestyle.org/java/FAQ.shtml#useragentheader What is special about static variables- (Found June 24, 2008 ) A: A static variable is shared by all instances of a class. Every instance has a reference to the same variable and can modify it directly. Static instances are declared with the static modifier.http://www.codestyle.org/java/FAQ.shtml#staticvariable What is the difference between static and final keywords- (Found June 24, 2008 ) A: The static and final modifiers have quite distinct purposes in Java, though they can be used in combination to declare class constants. When the static modifier is applied to a variable or method it belongs to all instances of the class and can be referenced in a static context. In other words, the class does not have to be instantiated before the variable can be read or the method called. That means that static variables must be assigned at compile time and static methods cannot reference...http://www.codestyle.org/java/FAQ.shtml#staticfinal Can I call super() from a static context- (Found June 24, 2008 ) A: The super() method can only be called in an object's constructor, it cannot be called in a static context. The only way to invoke the super() method is to place this statement in the first line of an object's constructor and instantiate the object. You can then invoke this method via the static void main(String) method, as below.http://www.codestyle.org/java/FAQ.shtml#staticsuper How do I call a static method in a separate class- (Found June 24, 2008 ) A: To call a static method in a separate class you should prefix the method reference with the name of the class it belongs to, known as the host class. For example, the String class has valueOf() methods to convert primitive values to strings:http://www.codestyle.org/java/FAQ.shtml#separatestaticcall Can I call a static method from an object reference- (Found June 24, 2008 ) A: Yes, it is possible to call a static method on an object reference using the same dot syntax as for a static class reference, as below.http://www.codestyle.org/java/FAQ.shtml#referencestatic |