Java class and instance variables memory usage in a web application?

I have a Java class in which every method was defined as static. So that I don't have to re-write the class, and then a good bit of code that depends upon it, I'm adding in some error reporting via an instance variable. However, Java doesn't seem to be able to access instance variables from class methods. I read Sun's description of class variables and am wary of just changing every method to an instance method in this class without a better understanding of how it would work in a web application.

According to (1), as I understand it, class methods share the same memory location for all instances of the object. So, in a web application, wouldn't that mean every process references the same memory address for a static method? And, in turn, each process would re-define all of the instance methods?

If I were to create a class variable to keep track of errors, wouldn't that introduce a situation where process A could trigger an error in process B? Can instance methods even access class variables?

Edit:

Let me clarify what I'm trying to accomplish with some example code.

First, my class:

public class MyClass { public int error = 0;

public String methodA() { // Do some stuff if (ret == null) this.error = 1; return ret; }

public static boolean methodB() { // Same thing but I can't access this.error here } }

Now my application:

MyClass myClass = new MyClass(); String aString = myClass.methodA(); if (myClass.error != 0) { out.print("What did you do!?"); return; }

-------------Problems Reply------------

class methods share the same memory location for all instances of the object

So do instance methods. The code for all methods of a class exists only once. The difference is that instance methods always require an instance (and its fields) for context.

If I were to create a class variable to keep track of errors, wouldn't that introduce a situation where process A could trigger an error in process B? Can instance methods even access class variables?

Yes and yes. This is why having non-final static fields is generally considered a bad thing.

Keep a static private instance

class Foo {
private static Foo myInstance = new Foo();

public static void MyPretendInstanceMethod() {
myInstance.doBar();
}

private void doBar() {
// do stuff here
}

}

Add synchronization as necessary.

Static methods can only access static class variables. They cannot access instance variables because static methods are not tied to any specific instance of the class.

You don't need to create a new instance of a class in order to access a static member or variable (as long as it's public). In your example, you can call methodB like this:

String bString = MyClass.methodB();

As you can see, no new instance had to be created.

But because methodA is not static, you must create an instance of the class in order to call it, like you did in your example:

MyClass myClass = new MyClass();
String aString = myClass.methodA();

Category:java Views:0 Time:2010-05-12

Related post

  • Terminology: one-word Java term for instance variables? 2011-05-05

    I'm looking for an alternativ OO/Java term of instance variables declared in a class (non-static), or more specifically in a Java class "decorated" with JPA annotations: @Entity @Table(name = "Departments") @IdClass(value = DepartmentId.class) public

  • How to measure Graphic Memory Usage of a WebGL application 2011-11-14

    I have seen several applications to profile DirectX and OpenGL applications, which measure Graphic Memory Usage. I were not able to find anything similar for WebGL. I am using WebGLInspector to debug WebGL applications, but Graphic Memory Profiling i

  • Suggestions for finding a memory leak in a web application running in Apache Tomcat 2011-06-14

    I've got an Axis2 web service that just fell over on a client site it's throwing the following exception: java.lang.OutOfMemoryError: unable to create new native thread I'm just pulling the logs off the site but in the interim I was wondering if anyo

  • Good tools to record network usage of a web application 2012-01-16

    I am about to start work on a project to reduce the bandwidth usage of a web application. We are going to implement several techniques such as delayed loading of Javascript files until they are needed to try and reduce the overhead of running the app

  • Any Java caches that can limit memory usage of in-memory cache, not just instance count? 2009-03-27

    I am looking for a simple in-memory (and in-process) cache for short-term caching of query data (but short-term meaning beyond request/response, i.e. session boundary). EhCache would probably work, but it looks as if it might not offer one thing that

  • Java's static/instance variables operation performance 2011-12-31

    While reading Jack Shirazi's 'Java performance tuning', I came across the following statement: "Instance and static variables can be up to an order of magnitude slower to operate on when compared to method arguments and local variables." Is this stil

  • Beginner Java question about instance variables 2011-05-15

    Possible Duplicates: Does “this” have any advantage? Do you prefix your instance variable with 'this' in java ? When assigning/referring to an instance variable, what's the difference between this.foo, and simply foo? Does the former give a performan

  • Java: Explain why instance variables are Non Thread Safe 2012-04-21

    When I read Head First Servlet and JSP, they say that instance variable is non-thread safe. I don't understand this statement so much. For example: I have a servlet which name is "action.java". Each time, each user's request is sent to server, contai

  • Java, Gradle, CI, and specifying memory usage limits 2014-08-08

    I'm running Robolectric test cases in a CI environment, which sets an upper limit of 4gigs across all containers. Since my test cases have grown, so has the memory usage apparrently. I can no longer run a successful test/build because of a process th

  • Java question about instance variables vs. local variables 2011-09-16

    Sorry if I'm missing something obvious here, but I'm confused about what's going on. I declare Workout mWorkout; as an instance variable at the top of my class. Up until this point, it hasn't been initialized. This code works: createWorkout returns a

  • Does anyone else think instance variables are problematic in database-backed applications? 2010-05-31

    It occurs to me that state control in languages like C# is not well supported. By this, I mean, it is left upto the programmer to manage the state of in-memory objects. A common use-case is that instance variables in the domain-model are copies of in

  • Memory usage of C++ / Qt application 2009-05-11

    I'm using OS X 10.5.6. I have a C++ application with a GUI made with Qt. When I start my application it uses 30 MB of memory (reported by OS X Activity Monitor RSIZE). I use this application to read in text files to memory, parse the data and finally

  • How to check the performance & memory usage of the flash application at run time? 2010-08-17

    I have a flash application which consist of Grid Components, Button, Label, and Combo-box Components. All these components are used more than 70 times(simultaneously) with in the application. So Its takes too much of memory. So How can I test the mem

  • How to determine memory usage in my .NET application 2011-01-24

    I have a website where some user can upload his dll. I need to create an instance from this dll and call a specific method. Moreover, I need to do it with several dll's in 1 process, so it is not an option for me to use System.Diagnostics.Process. Is

  • Too Much Memory Usage for Windows form Application 2011-12-22

    I Made an application to download a folder from a given sharepoint site, but its consuming memory above 600,000K once i click on Connect button, anyone can give suggestion to improve my code ? I Tried to debug my form and problem is coming at method

  • Why does the memory usage of a .NET application seem to drop when it is minimized? 2008-10-06

    For example, launch Paint.NET. Then have a look on its memory usage with Task Manager: on my computer, it uses 36Mb. Then minimize Paint.NET: now it takes only a few more than 1Mb. This happens with every .NET Application. What happens when a .NET Ap

  • Is it possible to track memory usage in a C# application without using a profiler? 2009-02-27

    I'd like to write a simple application which keeps track of its current memory usage, number of created objects etc. In C++ I'd normally override the new operator, but for obvious reasons I can't do this in C#. Is there any way to do this without usi

  • How do keep memory usage and size of application down (iPhone)? 2010-12-21

    I am currently working on a personal iPhone project that is very audio and graphic-intense and I have therefore been quite conscious of how much of a footprint my application has (in terms of both memory usage while active and/or in 'multi-tasking mo

  • Most Accurate Way to Get Memory Usage for Silverlight Browser Application 2011-11-04

    Currently i'm tracking down a memory leak issue in a Silverlight 4 application. I can watch the memory used by iexplorer.exe increase gradually using Task Manager. However, I am looking for a way within the Silverlight code to tell how much memory th

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.089 (s). 11 q(s)