Convertion of class variable (static) into instance variable(non-static)!

Dear all,
got a slight different question.
Is that possible to convert class variable (static) into instance variable(non-static)?
If so, how to make the conversion?
Appreciating your replies :)
Take care all,
Leslie V
http://www.googlestepper.blogspot.com
http://www.scrollnroll.blogspot.com

JavaDriver wrote:
Anything TBD w.r.to pass by value/reference (without removing 'static' keyword)?Besides the use of acronyms in ways that don't make much sense there are two other large problems with this "sentence".
1) Java NEVER passes by reference. ALWAYS pass by value.
2) How parameters are passed has exactly zero to do with static.
Which all means you have a fundamentally broken understanding of how Java works at all.
Am I asking something apart from this?
Thanks for your reply!
Leslie VWhat you're asking is where to find the tutorials because you're lost. Okay. Here you go [http://java.sun.com/docs/books/tutorial/java/index.html]
And also for the love of god read this [http://www.javaranch.com/campfire/StoryPassBy.jsp] There is NO excuse for not knowing pass-by in Java in this day and age other than sheer laziness.

Similar Messages

  • What is the difference between instance variable and class variable?

    i've looked it up on a few pages and i'm still struggling a bit maybe one of you guys could "dumb" it down for me or give and example of how their uses differ.
    thanks a lot

    Instance is variable, class is an object.What? "Instance" doesn't necessarily refer to variables, and although it's true that instances of Class are objects, it's not clear if that's what you meant, or how it's relevant.
    if you declare one instance in a class that instance
    should be sharing within that class only.Sharing with what? Non-static fields are not shared at all. Sharing which instance? What are you talking about?
    if you declare one class object it will share
    anywhere in the current program.Err...you can create global variables in Java, more or less, by making them static fields. If that's what you meant. It's not a very good practice though.
    Static fields, methods, and classes are not necessarily object types. One can have a static primitive field.

  • Instance/Class Variables/Methods

    Can someone please give a CLEAR explanation on what is a Instance Variable, Instance Method, Class Variable, and Class Method? Thanks a lot!

    instance method and variable is one that requires an instance to be created before they can be used.
    Class method and variables don't need an instance. They have the static qualifier.
    For example
    public class Test {
        public String instanceVar;
        static public String classVar;
    }Now to use instanceVar you have to do this
    Test test = new Test();
    test.instanceVar = "";To use classVar
    Test.classVar = "";Another way of looking at it is that each instance of a class (created using new) has its own copy of instance variables.
    Multiple instances of a class however share the same copy of class variables.
    The same pretty much goes for methods.

  • Initial values to instance & class variable

    I read in the book that instance & class variable definitions are given an initial value depending on the type of information they hold:
    Numeric variable - 0
    Characters - '\0'
    Boolean - false
    Objects - null
    But when I try to compile a class without giving explict value to the variable I get error.
    So why do I need this initial value - if it can't be used any way?

    Sorry� I'll try to make my question clearer.
    As I wrote in my previous message, I read in the book that instance & class variable definitions are given an initial value depending on the type of information they hold:
    Numeric variable - 0
    Characters - '\0'
    Boolean - false
    Objects - null
    From the above I understand that whenever I declare a variable without assigning it a value, it automatically has an initial value.
    For example:
    Class Myclass(){
    Int x;
    Static int z;
    Void fooMyClass(){
    Int y;
    I understand from what I read in the book, both x, y & z - get default values of zero. Is that correct?
    if it is correct, why can't I use these values that were assign as default?

  • Create an instance of my class variable

    Hello all,
    I'm a newbie to iPhone/iPad programming and am having some trouble, I believe the issue is that I'm not creating an instance of my class variable.  I've got a class that has a setter and a getter, I know about properties but am using setters and getters for the time being.  I've created this class in a View-based application, when I build the program in XCode 3.2.6 everything builds fine.  When I step through the code with the debugger there are no errors but the setters are not storing any values and thus the getters are always returning 0 (it's returning an int value).  I've produced some of the code involved and I was hoping someone could point out to me where my issue is, and if I'm correct about not instantiating my variable, where I should do that.  Thanks so much in advance.
    <p>
    Selection.h
    @interface Selection : NSObject {
      int _choice;
    //Getters
    -(int) Choice;
    //Setters
    -(void) setChoice:(int) input;
    Selection.m
    #import "Selection.h"
    @implementation Selection
    //Getters
    -(int)Choice {
      return _choice;
    //Setter
    -(void)setChoice:(int)input{
              _choice = input;
    RockPaperScissorsViewController.m
    #import "RockPaperScissorsViewController.h"
    @implementation RockPaperScissorsViewController
    @synthesize rockButton, paperButton, scissorsButton, label;
    //@synthesize humanChoice, computerChoice;
    -(void)SetLabel:(NSString *)selection{
              label.text = selection;
    -(IBAction)rockButtonSelected {
    //          [self SetLabel:@"Rock"];
              [humanChoice setChoice:1];
    </p>
    So in the above code it's the [humanChoice setChoice:1] that is not working.  When I step through that portion with the debugger I get no errors but when I call humanChoice.Choice later on in the code I get a zero.
    -NifflerX

    It worked, thank you so much.  I put
    humanChoice = [[Selection alloc]init];
    In viewDidLoad and it worked like a charm.  Thank you again.
    -NifflerX

  • How to convert an int variable into String type

    hi everybody
    i want to know how to convert an interger variable into string variable
    i have to implement a code which goes like this
    Chioce ch;
    for(int i=0;i<32;i++)
    // here i need a code to convert the int variable i into a string variable
    ch.add(String variable);
    how do i convert that int variable i into a String type variable??
    can anyone help me?

    Different methods:
    int a;
    string s=a+"";or
    String.valueOf(int) is the better option because Int.toString() generated an intermediate object to get the endresult
    Ema

  • Static class variable

    Folks:
    I am little confused by static class variables in Java. Since Java doesn't have global varaibles it uses static variables instead. Please take a look at the following code and please tell me what goes wrong.
    /********** CONTENT OF Stack.java ***********/
    import java.util.Stack;
    public class StackClass
    {   static  Stack stack = new Stack (); }
    /********** CONTENT OF Test1 .java ***********/
    public class Test1
    public static void main( String[] args )
    StackClass.stack.push("Hello World");
    /********** CONTENT OF Test2.java ***********/
    public class Test2
    public static void main( String[] args )
    System.out.println( "Top on stack is " + StackClass.stack.peek() );
    I execute the above programs in the sequence of StackClass.java, Test1.java and Test2.java. I think in Test1.java after I push one element to the stack it should still be in the stack in Test2.java But I got :
    java.util.EmptyStackException
         at java.util.Stack.peek(Unknown Source)
         at Test2.main(Test2.java:16)
    Exception in thread "main"
    Can anybody give me a hint?
    Thanks a lot !

    After you run StackClass, the JVM (java.exe) ends and all the classes are unloaded including StackClass
    When you run Test1, StackClass is loaded, an item is pushed on the stack, and then the JVM exits and all classes are unloaded including StackClass
    When you run Test2, StackClass is loaded, and you get an error because the StackClass which was just loaded has no items in it.

  • Static class variable doesn't store ?

    Hi,
    I'm a beginner in java.
    I defined a static class variable (nodes) in th following code :
    public class MySOMapp extends javax.swing.JFrame {
         private static final long serialVersionUID = 1L;
         private static SOMTrainer trainer;
         private static SOMLattice lattice;
         private static SOMNode nodes;
         private static Hashtable nwordf; 
         public MySOMapp(String args[]) {
              SOMNode nodes = new SOMNode();          //.....But a method of this class, nodes comes null. methos definition :
    private void makesame() {I don't understand why the nodes is null in makesame method which is in the same class with nodes ?
    thanks,
    Yigit

A: static class variable doesn't store ?

thankyou. I solved the problem. But why static fields
are not good idea ? How can i get rid of these...Remove the static word :) Seriously, try to develop a technique to get swiftly out of the static main method. Like this:
class MyClass {
    MyClass() {
        // Your code here instead of in main method.
    public static void main(String[] args) {
        new MyClass();
}

thankyou. I solved the problem. But why static fields
are not good idea ? How can i get rid of these...Remove the static word :) Seriously, try to develop a technique to get swiftly out of the static main method. Like this:
class MyClass {
    MyClass() {
        // Your code here instead of in main method.
    public static void main(String[] args) {
        new MyClass();
}

  • Convert to class instance into string/binary

    Hello friends,
    is there a utility to convert a class instance into string or binary.
    regards
    kaushik

    Take a look at CL_ABAP_STRING_UTILITIES
    Best!
    Jim

  • Reassign class variable to a blank instance of its class

    When I declare a class variable
    private var myVar:MyClass = new MyClass();
    It creates a new instance of that class that will trace as [Object MyClass].
    However, as soon as I assign it to something
    myVar = thisOtherVar;
    It will now trace as [Object thisOtherVar].
    I need to get it back to where it's just an "empty" blank class instance, if that makes any sense at all.
    How do I go about doing that? Thanks!

    I'm brand new to OOP - I've been watching iTunes U videos and reading books but it's still all pretty abstract in my mind and it's difficult for me to pin point how all that stuff translates to actually writing good code.
    Basically, it's an open ended kid's dictionary. You drag the lettes to drop boxes, touch the check to see if it's a word, and then a picture representing the word pops up on the screen.
    All of it works except if you try to remove a letter that's already been placed. Originally, I wanted the check button to do a sweep of the dropped letters and see if they had formed a word - but I couldn't figure out a way to do that. So then I started tracking movements of letters in real time, and that's when it got really confusing. I think I just need to start over and try to make the code a bit cleaner. Right now this is what I've got:
    if (currentUsedBlank is BlankSquare) {  //if they have removed a letter that has already been dropped
                    if (currentUsedBlank != currentBlank) { //and it's been placed back on a different square
                        currentUsedBlank.letter = null;
                else {
                    currentUsedBlank.letter = null;
                if (this.dropTarget.parent is BlankSquare && currentBlank.letter == null) {
                    SpellingGlobals.lettersInBlanks[SpellingGlobals.lettersInBlanks.length] = this;
                    this.x = this.dropTarget.parent.x + 10;
                    this.y = this.dropTarget.parent.y - 10;
                    var myClass:Class = getDefinitionByName(getQualifiedClassName(MovieClip(this))) as Class;
                    var newLetter:MovieClip = new myClass();
                    newLetter.x = currentLetter.startPoint.x;   
                    newLetter.y = currentLetter.startPoint.y;
                    newLetter.letter = currentLetter.letter;
                    newLetter.reference = currentLetter.reference;
                    newLetter.startPoint.x = currentLetter.startPoint.x;
                    newLetter.startPoint.y = currentLetter.startPoint.y;
                    newLetter.isVowel = currentLetter.isVowel;
                    currentBlank.letter = currentLetter.letter;
                    if (SpellingGlobals.isCaps == true) {
                        newLetter.txt.text.toUpperCase();
                        if (SpellingGlobals.isColor == true) {
                            if (newLetter.isVowel) {
                                newLetter.txt.textColor = SpellingGlobals.colorVowel;
                            else {
                                newLetter.txt.textColor = SpellingGlobals.colorCon;
                    MovieClip(root).addChild(newLetter);
                    SpellingGlobals.copiedLetters[SpellingGlobals.copiedLetters.length] = newLetter;
                    currentBlank.alpha = 0;
                else {
                    this.x = MovieClip(this).startPoint.x;
                    this.y = MovieClip(this).startPoint.y;
                this.stopDrag();

  • Class variables x static ones

    I am with a doubt. I would like that the variables I have in the c program behave as class variables and it do not occur, tghey behaves more like static variables. By any chance any one knows if its possible that the c internal variables behave like class variables?
    I am putting down the helloworld example and the output.
    --------- HelloWorld.c -----------------------------
    #include <jni.h>
    #include "HelloWorld.h"
    #include <stdio.h>
    int test = 0;
    JNIEXPORT void JNICALL
    Java_HelloWorld_hello(JNIEnv *env, jobject obj) {
    printf("Hello world! %d\n", test++);
    return;
    ------HelloWorld.java------------
    public class HelloWorld {
    public native void hello();
    static {
    System.loadLibrary("hello");
    public static void main(String[] args) {
    HelloWorld hw = new HelloWorld();
    hw.hello();
    hw.hello();
    hw = null;
    HelloWorld hw2 = new HelloWorld();
    System.out.println("-----------");
    hw2.hello();
    hw2.hello();
    ---------- output-----
    /testeHello @varda : java HelloWorld
    Hello world! 0
    Hello world! 1
    Hello world! 2
    Hello world! 3
    ---- I would like it was.....
    /testeHello @varda : java HelloWorld
    Hello world! 0
    Hello world! 1
    Hello world! 0
    Hello world! 1

    Thanks. I thought in let the variables I need in the Java class... However I think it would not work, I have some pretty complex structures in C that I would need to port to java, without to say I have some sockets and pointers also. First I really don't know if it is feasible, and second even if it is feasible It would be a lot of work, I know I am lazy :). The problem is that some of these need to be global, I really can't do in other way.
    Actually what I am doing is exactly create a vector to each global variable I need and I am also putting an ID at the Java class. In this way the C method know who is calling it and uses the id as index of the vector. Well It works, but I think it is pretty ugly (we call this kind of thing as "Hammer" :) and I would like to use some thing better ;).

  • Assign a value to class variable

    I want to define a class variable in class. And I want all subclasses of that class ot have a different value for that class variable. How can I do that?
    public class BaseClass {
      public static String tableName = "";
    }Now if I have a ClassA and I want to assign a value like this :
    public class ClassA extends BaseClass {
      tableName = "location";
    } I got an error message.
    I can move it in a static initializer block but then it will only work when the class is loaded. In my case its possible i want to get this value without loading the class.
    Ditto if i move it to constructor.
    Any input? Thanks

    Are you saying that if i have 2 classes ClassA and
    ClassB inherited from BaseClass, then both are
    sharing the same copy of 'tableName' staticvariable?
    If yes then I should go with an instance variable.No, I am saying that you can easily declare a
    tablName in A and another tableName in B.
    A.tableName will be shared between all the instances
    s of A. B.tableName will be shared between all the
    instances of B. And BaseClass.tableName is
    irelevant. I think you try to use
    BaseClass.tableName as some kind of template for
    sub-classes, but this does not happen: you need to
    declare tableName again and again in each subclass.
    IThanks for clarifying. Each class needs to have a variable "tableName" and it needs to have one copy of this variable for all of that class's objects. 2 classes will not have the same value of this tableName variable.
    Thats why I was defining it as static variable. And I define it in BaseClass so that I dont have to define it again in each subclass.
    Is there any better way? Thanks

  • How to access the parent class variable or object in java

    Hi Gurus,
    I encounter an issue when try to refer to parent class variable or object instance in java.
    The issue is when the child class reside in different location from the parent class as shown below.
    - ClassA and ClassB are reside in xxx.oracle.apps.inv.mo.server;
    - Derived is reside in xxx.oracle.apps.inv.mo.server.test;
    Let say ClassA and ClassB are the base / seeded class and can not be modified. How can i refer to the variable or object instance of ClassA and ClassB inside Derived class.
    package xxx.oracle.apps.inv.mo.server;
    public class ClassA {
        public int i=10;
    package xxx.oracle.apps.inv.mo.server;
    public class ClassB extends ClassA{
        int i=20;   
    package xxx.oracle.apps.inv.mo.server.test;
    import xxx.oracle.apps.inv.mo.server.ClassA;
    import xxx.oracle.apps.inv.mo.server.ClassB;
    public class Derived extends ClassB {
        int i=30;
        public Derived() {
           System.out.println(this.i);                  // this will print 30
           System.out.println(((ClassB)this).i);  // error, but this will print 20 if Derived class located in the same location as ClassB
           System.out.println(((ClassA)this).i);  // error, but this will print 20 if Derived class located in the same location as ClassA
        public static void main(String[] args) { 
            Derived d = new Derived(); 
    Many thanks in advance,
    Fendy

    Hi ,
    You cannot  access the controller attribute instead create an instance of the controller class and access the attribute in the set method
    OR create a static method X in the controller class and store the value in that method. and you can access the attribute by 
    Call method class=>X
    OR if the attribute is static you can access by classname=>attribute.
    Regards,
    Gangadhar.S
    Edited by: gangadhar rao on Mar 10, 2011 6:56 AM

  • Private or Protected access for super class variables

    What is the best practice...
    Assume there is a class hierachy like
    Person (Super class) / StaffMember/ Professor (Sub class)
    1) The best way is to keep all the instance variables of each and every class private and access the private variables of super classes through subclass constructors (calling "super()")
    Ex:-
    public class Person {
    private String empNo;
    public Person (String empNo) {
    this.empNo = empNo;
    public class Professor extends Person {
    private String ........;
    private int ...........;
    public Professor (String pEmpNo) {
    super(pEmpNo);
    OR
    2)Changing the access level of the super class variables into "protected" or "default" and access them directly within the sub classes...
    Ex:-
    public class Person {
    protected String empNo;
    public Person () {
    public class Professor extends Person {
    String ........;
    int ...........;
    public Professor (String empNo) {
    this.empNo = empNo;
    Thank you...

    i'd think that you'd be better off relaying your initial values through the super class's constructor that way you'll get cleaner code, there's a possibly of inconsistency with option 2. i.e. you can then write code in your super classes to generally handle and properly initialize the instance variables while in the case of option 2, you'll have arbitrary constructors performing arbitrary initialization procedures

  • Class variable declaration

    Hello.
    Just looking through some code and I see a class variable declared as so...
    static final MessageDigest digestFunction;
    static { // The digest method is reused between instances to provide higher entropy.
            MessageDigest tmp;
            try {
                tmp = java.security.MessageDigest.getInstance(hashName);
            } catch (NoSuchAlgorithmException e) {
                tmp = null;
            digestFunction = tmp;
        }I have never come accoss a variable being declared in this way and it looks strange. The use of static before the opening brace is strange to me, and just the fact that this is not inside the constructor. Would this code be run after or before code inside the class constuctor?
    Thanks for any help, just a little confused!

    Sir_Dori wrote:
    Ah yes, of course it could not be within the constructor as it is static, sorry.
    So when would the class be loaded? Could this be the first time the ClassName.staticBlockVar is accessed, Most likely, yes. The first time something else uses the class. Classloading in Java is lazy.
    Also, is the code not reevaluated every time it is referenced ?No. Only when the class is loaded (actually, when it's initialized, but usually it boils down to the same thing)
    You can write some simple tests for this, by writing a constructor and a static initializer, then instantiating it twice, for example. Don't be afraid to just play around with code to test assumptions. I find JUnit very handy for experimenting with assumptions.

  • Maybe you are looking for