I wonder why Java 5 and above provide a printf-style formatter using a static method in class String like this:
public static String format(String format, Object... args)
instead of
public String format(Object... args)
so ... |
While working on my previous problem,
http://stackoverflow.com/questions/950636/java-jar-class-not-found-exception
I noticed something odd. the class that can not be found is referenced from main. Now if i try to create an instance of the ... |
In java Can objects be created with both static memory allocation and dynamic memory allocation?
|
I was reading Joshua Bloch's "Effective Java Programming Language Guide".
He explains static factory methods could be used to avoid unnecessary duplicate objects.
I haven't quite understood this.
Could ... |
As shown below, there are two straightforward ways I could make a stream copier (bar introducing Apache Commons or similar). Which one should I go for, and why ?
public class StreamCopier ...
|
I have following method that I would like to make shorter or faster if nothing else. Please all comments are welcome:
Bellow method takes a date object, formates it ("EEE ... |
I have an encryption utility method that relies on an external dependency --- a cipher key which is being retrieved from a property file. The way it can be retrieved in ... |
|
In Java static methods are created to access it without any object instance. It makes some sense to me. But recently I came across one strange thing that, static method in ... |
In the following code, where should sc be constructed? Without the line "sc = new SClass()", I get a null pointer exception, but I'm not sure if that's the right ... |
So I've got a GWT app and I'm trying to invoke a function to change the app's view from an outside button. Normally this is called with as "this.internalFunction('string')". ... |
Is it possible to get an object that invoked static method in this method?
I have this code:
class A{
static void foo(){
}
}
A a = new ...
|
is Calling a static Java method ( a factory class method ) creates an object of that Class ?
I mean a static method returns a value let's say an Array's size ... |
From within the class, how do I access a static variable explicitly? (Is the best practice to access static variable explicitly eg. using static.staticVar)
The below works
class Something {
...
|
I am writing a junit of CustomerHelper which internally calls the method of AccountHelper object. The CustomerHelper is creating the
AccountHelper object with new operator inside one of its ... |
In a recent question, someone asked about static methods and one of the answers stated that you generally call them with something like:
MyClassName.myStaticMethod();
The comments on that also stated that you could ... |
While browsing through some code for a library I'm using, I came across this code snippet:
public class SomeClass {
private static final class Null {
...
|
The following simple code snippet is working fine and is accessing a static field with a null object.
final class TestNull
{
public static int field=100;
public ...
|
The following simple program retrieves the current year (2011) from the system and then simply subtracts 1950 from the current year.
package calculation;
import java.util.Calendar;
final public class Main
{
private static ...
|
As per my knowledge, static members can be accessed directly. But if someone wants to access it by creating object of it, is this possible in Java?
|
Everytime FutureContent.future is called why will a new MyFuture class not be created and assigned to future ?
FutureContent.future
public class FutureContent {
public static MyFuture ...
|
Let's consider the following simplified Resource hierarchy:
public abstract class Resource {
static public boolean accepts(String resource);
}
public class AudioResource extends Resource {
static public boolean accepts(String ...
|
No, changing the variable to transient didn't help. Now I can't even invoke methods on the Remote Object itself remotely, let alone the methods on the private instance transient variable defined in the Object accessed remotely. Though local access worked a treat. It's back to the drawing board. I suspect there's some fundamental flaw in my Remote design. Any more tips ... |
|
If you place it in the context it will be lost but can be recreated each time the context is reloaded. The down side it that you can't enforce it's immutability - anyone that can see it can update it. Depending on your setup, making it globally available (eg like a singleton) may not refresh with context restarts (don't confuse this ... |
I'm assuming by static object you mean a static reference to an object, such as: private static SomeObject myReference = new SomeObject(); What this does is make the same reference available to all instances of the same class. So if the above line of code was in a class called MyClass, each instance of MyClass that you created with a 'new ... |
I have a class JLog to log all the messages to a file. The JLog class is in a jx.war file which is deployed to the application server JBoss. If the jx.war is deployed once, the messages printed to the file only show up once, which is fine. But if the jx.war is deployed twice, the messages printed to the file ... |
In an object-relational mapping model, one would have an instance of an object mapped to a row in a relational table. To manage a collection of instances, a separate object manager instance could be created to: get a list of instances, save a list of instances, etc. An alternative would be to use static methods at the instance's class level to ... |
The object references are static ( meaning no instance required ); If I do this (and it runs) : class StaticObjects { public static Larry l = new Larry(); public static Curly c = new Curly(); public static Moe m = new Moe(); public static void main(String[] args) { l.dostuff(); c.dostuff(); m.dostuff(); } } class Larry { public static void dostuff() ... |
Is object creation dynamic or static in Java? - This is an interview question asked to me. I googled it and found an article which says - Object creation is always a dynamic operation in Smalltalk-80, Java, and Eiffel. But, I don't understand the difference between a static and a dynamic creation and how it is said Java does dynamic object ... |
|
|
Yes, that would work. However, I would like to point out that there are other options depending on other things. Here are a few things you might want to consider: What is the purpose of this HashMap? When does this it need to be created (i.e. what is the earliest method in the program that needs to access it)? What class ... |
Hi, I have a simple doubt. Pls clarify, [B]CODE:1[/B] int a = 10; static b = 20; // creating objects for this class Test t = new Test(); Test t1 = new Test(); In the above coding, 't' will have its own copy of 'a' and 't1' will have its own copy of 'a'. Both 't' and 't1' will share the ... |
Greetings everybody! I'm stuglling with this little thing on a Java programme. I have the following class public class Dice { static int start; static int end; public Dice(){ //System.out.println("Dice constructor called"); // start = 1; end = 6; // } // public int roll() { return (int) ((10 * Math.random()) % end + start); } } I want to have ... |
The term "static object reference" doesn't make sense to me (maybe it's too early in the morning) because using a static member means that there is no object involved. Maybe what you are asking is what's the different between accessing a static method vs. accessing a non-static member. Non-static methods are called using a object reference. For example: MyClass myObject = ... |
There is only one copy of the static variable ACTION_NAME in this program. It looks like the author of this code believed each base class would get its own copy -- this is not the case. Its value depends on which of the two subclasses is loaded second. This is horrible code -- a horrible thing to do. If this is ... |
It means the the compile-time type of the instance is used to bind the method invocation to a method at compile time, it is not bound at run time polymorphically . The instance itself is never used to bind the method invocation to a method, thats why a NullPointerException isn't thrown when a static method is called using a null reference. ... |
|
Hi All, In many real time applications, I observe the object creation is in the static method. And all the methods in that class will be non static. And all non static methods is used in the other classes like Classic.getInstance().getName What is the main advantage of doing this. TO my knowledge we can do this another way also. Just make ... |
The Java lanuguage supports non-object-oriented design and code as well. Everything is not "automatically" OO simply because it was written in Java. I have seen "extremely anti-object-oriented" servlets with everything in the world (100's of statements) crammed in its service() method. I would say, using "static" in certain places might be considered anti-OO design. Using it in other places might not ... |
I am writing business logic for a web application in which web tier is done using JSF. My business logic is pretty much independent of UI aspects. In few of my logic classes an error message bundle object is defined as: /** The error message bundle object */ private static final AppResourceBundleUtil ERROR_MSG_RESOURCE_BUNDLE = new AppResourceBundleUtil ( AppLogicConstants.APP_ERRORS ); Where AppResourceBundleUtil's ... |
Hi this time I,m with some differnet type of confusion. as we know that garbage collector in java wroks only with those memory which are allocated by which are allocated by new() in heap; but we also know that static data get s their allcation in static storage independently regrdless to objects. so the prblem is that how this type of ... |
When the compiler sees a reference (like t) used to call a static method (like sleep()), all it looks at is the declared type of the reference variable. Since t is a Thread, t.sleep(20) is considered exactly equivalent to Thread.sleep(20). The actual value at runtime (which the compiler doesn't know) is completely ignored. Yeah, it's weird. But that's the way the ... |
|
I am facing one weird problem when accessing a static object instance, Test.java: import java.util.*; public class Test { private static Test instance = new Test(); private static Map map = new HashMap(); private Test() { enter(); } private void enter() { map.put("dummy1","dummy2"); } public static Test getinstance() { return instance; } public static void display() { System.out.println("Key = ... |
Are you asking whether the act of passing the reference to the method is thread-safe, or are you asking for a blanket assurance that anything the method does with the object is thread-safe? The answer to the second question is "Not necessarily" and the fact that the method is static is irrelevant to the issue of thread safety. I believe the ... |
hi, i have a static variable defined in my util class public static final Object IN_QUEUE = "In Queue"; i see that FAUtil.IN_QUEUE.toString() threw null pointer exception and after bouncing the container ( the code is part of a web application deployed on OC4J) it started working again. I did see this issue once or twice during the development time but ... |
Hi, I have created singleton object. It contains couple of static methods and non-static methods. I have created one Logger class object, while initializing the singleton object. In the static method i am calling the member object method to do some operation. The sample code given below. The code is working fine. My question here is, Does the singleton class can ... |
Hi, I have a question of sharing DB Connection object across different servlets of the same webapp. The current design is as follows. Class 1 provides methods to connect to DB. Methods of Class 1 are not static. (For each query it does a new con.createStatement()). Class 2, 3, 4 etc declare static instance of Class 1. During servlet context initialization ... |
|
Hi folks; I have kind of a design/philosophical question (I think ) Lets say that I have a timer that I'd like to turn on and off at will from any portion of my program. The timer fires and activates an external peripheral via a socket connection. The two ways I have found to do this are to: 1) Make the ... |
|
In Java static methods are created to access it without any object instance. It makes some sense to me. But recently I came across one strange thing that, static method in Java can also be accessed through its object instance. This looks pretty wierd to me. Does anyone of you know why this feature is provided by Java? Whats the significance ... |
There is no such thing as a "static object". You can certainly declare a static variable: private static File image; And you can certainly assign an object to that variable: private static File image = new File("kitty.jpg"); However that doesn't make that File object a "static object". It just makes it an object which happens to be referred to by a ... |
I have this code that i'm using to test if my program works, but all the Account type objects are somehow getting the same data inserted. Java Code: Account acc1 = new Account(); Account acc2 = new Account(); Account acc3 = new Account(); acc1.insert("AAA", 1, 2, 3, 4, 5, 6); acc2.setCode("CCC"); acc3.setCode("BBB"); It prints out : BBB 1 2 3 4 ... |
The (values of) fields of a particular object are assigned to the nonstatic fields of the class via constructor, when the object is initialized. Now what if I want to read in the values of these fields of the objects from the user? For example Bicycle is a class with nonstatic fields viz. gear and speed. Let mountainBike an object of ... |
It sounds like you're confusing static and final. If a variable is static but not final, you are free to adjust its value all you want at runtime. You haven't tried this yet, have you? I will say that it's generally considered bad practice to expose fields as public and allow them to be modified externally, but there are no mechanisms ... |
|
|
|
Objects are not static,the whole defintaion of being static is that it does not require a class instance. If u declare a method or a variable as static it does not require an object reference to use it.As it is stored in the stack directly.And no instace of a class(Object) is required. For a non static method or variable u use ... |
I want to have an object in my application that is global, and changeable to all the other classes/objects inside the program. I was wondering if this is what a static class is? AFAIK, static classes mean that there are only ever one instance of them running and you can access and change the values from anywhere in the program. Is ... |
Okay I've created an application to create and monitor a number of other applications. My contorller application consists of a main method and a number of static methods and objects. I'm trying to create tests to test the functionality of the application. The problem I have is it's static. once I run the main method once variables get set, timers started, ... |
... } Why would one ever need to create an instance of a class whose members are all static? This class is opening a socket and writing to it and storing the message using a thread. I am new to socket programming. All calls in the rest of the code reference the ConnectionManager members through the class name. |
I think when there is common behaviour by all objects that are created from a class. Those common behaviour could be exposed as a static method, such that all objects will have a way to expose the behaviour. by saying that class.staticmethod(); Am i right . Identifying the common behaviour, is the major task , while defining static methods i should ... |
The cfg object only get initialized/changed in the begining of the execute function, nowhere else. The iniciaTabelaConfig(); takes a bit of time, so I would like it if it would not be executed more then once, but some time after Tomcat starts (usually a few days) the cfg object becomes null. Why does this happen? Shouldn't the fact that it is ... |
There's no such thing as a static object, so the question doesn't arise. An inner class can be labelled static but that's about it's relationship with the enclosing class - an instance of a static inner class isn't associated with any particular instance of the enclosing class. A class might be called static if there's no instance fields or members but ... |
Final static objects are special-cased if they are primitive types or Strings. In this case they can be described as constants, and the compiler doesn't necessarilly create a slot for them, it substitutes the value into the code as literals. But when they're object types other than Strings they aren't treated much differently from non-final static. A slot for the reference ... |
Is there a problem with coding the following?: class A { public static final A static_final_a_obj = new A ( B.static_final_b_obj ); private B b; public A ( B b ) { this.b = b; } } class B { public static final B static_final_b_obj = new B ( A.static_final_a_obj ); private A a; public B ( A a ) { ... |
|
Ahhh, Thanks, I didn't realise that there were 2 JVM's created, I had thought that the modified value would carry on to the next object, but if the JVM is terminated at the end of the first object and a new JVM started to instanciate the next object, this makes sence! Thanks, Alan K. |
Hi everyone, I am working on a project and I kept running into an odd problem where my code would compile but when I went to run the code I would get an exception. I finally narrowed my problem down to a small fragment. Here is an example. class Mut{ static Mut Fido = new Mut(); Mut Sammy = new Mut(); ... |