About static

hi friends,
I have a doubt.Static variables or class variables are variables which are common to all objects,which implies class variables are blueprints charecteristics for the objects.
So,I agree it should be acessed only by class name as Classname.Staticvariable
but
new Classname.staticvariable is also allowed which i strongly disagree bcoz we use object to change the class's charecteristics which is something not allowed.
My doubt is why they are allowing to change

public class StaticDemo {
public static String STATSTRING = "I'm a static
c string: use me without instantiating a StaticDemo
object";
     public static void staticPrintMethod() {
System.out.println("I was printed from a static
ic method without object instantiation");
System.out.println("I don't return any value as my
my signature is 'void'");
     public static String getSTATSTRING() {
          return STATSTRING;
public class Scaf {
     public static void main (String args[]) {
          //print static String
          System.out.println(StaticDemo.STATSTRING);
          System.out.println();
          //call staticPrintMethod()
          StaticDemo.staticPrintMethod();
          System.out.println();
          //call getSTATICSTRING()
          System.out.println(StaticDemo.getSTATSTRING());
}these two examples should help...I dont mean it should not be modified but i mean it should not be allowed to be modified using instance,
as
new StaticDemo().STATSTRING="hi";

Similar Messages

  • Confused about static thread safe.help me

    sample codes look like follows:
    public static class test
          public static method1(Object obj)
                //some operations  
    }my question is that is method1 is thread safe?

    Thread safety isn't about methods and it isn't about static. It's about being carefully about access to resources which may be accessed by more than one thread, and in particular may be changed by one thread and accessed by another.
    Local variables are always thread safe, and there's no problem about serveral threads executing the same block of code concurently.
    Thread safety is, in particular, about data items that are linked in some way so that there can be problems if one thread updates one piece of data on the basis of an out of date version of another.
    Databases are, generally, designed to cope with that kind of thing.

  • About static member class?

    I can unstand the technical detail about static member class. But i still have a question about it.The questionis when shall we use it when we design a poject. In another word, in which situation, static member class will show its advantage? Thanks a lot.

    http://search.java.sun.com/search/java/index.jsp?qp=&nh=10&qt=static+nested+class&col=javaforums

  • Help,about static

    Today i wonder what i believe in always about static.....
    public class Test {
    public static int i = 1;
    public static void set() {
    i = 1000;
    public static void main(String[] args) {
    set();
    after executing Test,then execute Test1
    public class Test1 {
    public static void main(String[] args) {
    System.out.println(Test.i);
    why the Test.i is 0 now,not 1000?

    Hi "seabit",
    I am assuming you are issuing the following command (or something similar):
    java Test
    and then, after the above finishes, you are issuing:
    java Test1
    If the above is correct, then you are executing two different JVMs, and they are totally unrelated. What the first JVM did has absolutely no effect on the second JVM. If you really want "Test1" to have access to the instance of "Test" that you created in the first JVM, you should use java's "serialization" mechanism. I suggest you do an Internet search for information about serialization in java -- there is lots of information available.
    Hope this has helped you.
    Good Luck,
    Avi.

  • Help me!!!(about "static")

    The program as the following:
    class Bowl {
    Bowl(int marker) {
    System.out.println("Bowl(" + marker + ")");
    void f(int marker) {
    System.out.println("f(" + marker + ")");
    class Table {
    static Bowl b1 = new Bowl(1);
    Table() {
    System.out.println("Table()");
    b2.f(1); //b2 has not defined
    void f2(int marker) {
    System.out.println("f2(" + marker + ")");
    static Bowl b2 = new Bowl(2);//b2 define
    I don't understand about the "b2.f(1)",because "b2(f1)" has not defined yet befor it's used.i don't konw wether the object of "static" can be used befor it's defined.
    Please help me resolve this problem!
    Thanks!!!
    yours
    spark

    When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. It makes it much easier to read.
    Static means assosated with the class. Java Tutorial.
    has not defined yet befor it's usedEither if it was not static, by the time the ctor is run, all of its member variables are about (but might not be initatlized).

  • Question about static properties in implementation (.m) file

    I've been using some static properties in my implementation files so they can be accessed by class methods like so for example:
    #import etc
    static Class foo; // <-- Here is where I've been defining static properties
    @implementation Bar
    - init {
    foo = [Class new]; // initialize property
    + getObject { // now any where else in my code I can use [Foo getObject]
    return foo;
    - dealloc {
    [foo release];
    I've been doing this so that in other classes, I can simply go like:
    [Foo getObject]
    Can someone give me some more details about what's going on here though? It's been working but I'm nervous because I don't know much about it.
    ie. What kind of property is it? (nonatomic, retain)? Is this bad practice?
    Thanks to anyone who can shed some light on this.

    First off, it's not a property. It's a static variable. And the code you have isn't a good idea. Static variables should NOT be set via instance methods.
    Think about the following code:
    // In some other class
    Bar x = [[Bar alloc] init];
    Bar y = [[Bar alloc] init];
    Think about what just happened. In the 'foo' class you have now created two 'foo' objects and the first one is now leaked.
    The proper way to initialize static variables is with the 'initialize' class method. This is only ever called once, the first time the class is referenced.
    static Class foo;
    @implementation Bar
    + (void)initialize {
    foo = [[Bar alloc] init];
    - init {
    // Do nothing with 'foo'
    + (Bar)getObject {
    return foo;
    - (void)dealloc {
    // Do nothing with 'foo'
    The only downside, sort of, is that 'foo' is never dealloc'ed except when the app exits. But this isn't typically a problem.

  • Question about static ip

    Here's the situation:
    I live in a house of about 30  tenants. The place uses a cisco WRVS4400N router. It seems that my  computer gets assigned a temporary ip automatically and then there will  be times when I get "kicked out" and my computer doesn't get connected  to the internet. Sometimes for few minutes and sometimes for hours.
    No  idea what happens there but could be that there are no ips left to  assign to my computer and someone else gets the ip address.
    Anyway I'm trying to find out if I can configure my computer with a static ip.
    The  maximum number of dhcp users was changed from 50 to 100 by me. IDK if  that means that I can assign a static ip from 1 to 100 but say I pick a  number 160 so the ip would be xxx.xxx.x.160.
    1. What are the steps I have to undertake to configure my computer on the router page? What info. do I have to modify?
    2. After I modify settings on the router page, do I have to change settings on my computer?

    Al,
    You do not need to change anything in the router. You can simply assign your PC a static IP address. If your DHCP pool is xxx.xxx.x.100-200, use something like xxx.xxx.x.99 on your PC to avoid IP conflict. (assign an address that is not used by the DHCP server) If your router is xxx.xxx.x.1, set that as the Default Gateway and Primary DNS Server on the PC. You can also use 8.8.8.8 as the DNS Server if you wish. The Subnet Mask should be 255.255.255.0
    See the following link for excellent instructions about giving a PC a static IP:
    http://www.howtogeek.com/howto/19249/how-to-assign-a-static-ip-address-in-xp-vista-or-windows-7/

  • Confusion About "Static"

    If I have
    public class abc
      private static final int x=1;
      public static def()
        final int y=2;
    }Since you can't make y static, and the method is static, does y become static automatically??

    1. static methods are faster as there is no
    polymorphismActually there is polymorphism from a Class P.O.V.
    2. it's nice for helper classes like java.lang.Math
    who have no instances.
    3. They are needed for Objects without public
    constructors (think Factory, Singleton, etc)The main reason for static methods (class methods) is to provide methods that are not instance specific. As phohmeyer mentioned classes like java.lang.Math have no instances because the needed funcitonality is provided by the class itself. There is nothing about the Math class that will change from instance to instance, so there is no need to ever instanciate one and use instance methods.
    Think about the following:public class MyFirstClass extends Object{
      static public void main(String[] args){
        System.out.println("Hello World.");
    }Normally, when you thing about using objects, you have to create one. However, how would you use MyFirstClass if it is the entry point of the system? In other words, where would you create an instance of MyFirstClass if your intent is to run MyFirstClass from the command prompt? Making main() a static (and public) method allows it to be called without creating an instance of MyFirstClass.
    Likewise, the line that prints "Hello World." is making use of a static member. Note, an instance of System is never created. However, you are still allowed to access the output stream of System becasue out belongs to the class (that is, it exists as long as the class exists).

  • Administration Panel Error and a Question about Static IPs

    Since there appears to be no other place to report errors within the latest generation of Linksys router firmwares, I thought the forums may be the best place.
    If you use remote access to your router's Administration management console, upon saving any changes you are sent to the
    "Your settings have been successfully saved." page. Upon clicking cancel it successfully attempts to route to the appropriate hostname but does not consider the port being used; therefore, unless you have your management console hosted on port 80 it does not bring you back to the right place.
    This is mostly an annoyance.
    My question is I'm wondering if it's possible to assign static IP addresses from the router.
    On my older (much older) Linksys routers (before Cisco bought them out) you could easily assign static IPs.
    I cannot seem to find a way to do with newer generations.  All suggestions recommend assigning static IPs from the
    network devices themselves, however that poses problems on modern mobile devices which don't let you do that,
    and for laptops that are brought into a lot of different networks it becomes an annoyance to change those settings manually.
    I have a EA4500 router.
    Solved!
    Go to Solution.

    You want to assign a specific ip address to your computers/network devices thru the router? You can use the DHCP reservation feature of this router.
    "A DHCP Reservation is a permanent IP address assignment.  It is a specific IP address within a DHCP scope that is permanently reserved for leased use to a specific DHCP client."
    Please check this link:
    http://www6.nohold.net/Cisco2/ukp.aspx?vw=1&docid=71dac52653fa4944ae5e4f94ebdf9586_17362.xml&pid=80&...

  • See this question about static object

    1)static A a =new A();
    2) A a1=new A();
    what is the difference between them?

    static A a =new A();
    When this line is written in another class say : class b;
    a is accessible without the need for declaring an object of b.
    i.e., b.a
    Not in the other case.
    Many factory classes and methods are accessed this way coz, the classes are private and abstract, so u cannot make an object of that class.
    For example:
    System.out.println() - here println() is a method, out is an object of some class that is declared static in the class System. if it were'nt static, then we had to make an object of the System class and then access the out object,
    hope this helps
    let me know

  • About static variable initialization

    Here is an exercise where you have to guess the output :-)
    public class MyClass{
        private static int x = getValue();
        private static int y = 5;
        private static int getValue(){
            System.out.print("Running getValue ");
            return y;
        public static void main(String[] args){
            System.out.println(x);
    }This code outputs "Running getValue 0" I don't understand why?

    because class initialisation will call getValue() before it initialises 7 to 5, thus setting x to the value y has before being initialised which is 0.
    What this tells you is that you should never program rubbish like that, and in general not rely on the value of uninitialised members.

  • About static class

    Dose Java has Static class?
    What is diff between static class and a final class which all method are static in java?

    Dose Java has Static class?yes, but only if the class is an inner class
    a static inner class does not have a reference to its enclosing class - because of this, a static inner class cannot use this to access any instance variable of the enclosing class
    however, a static inner may access all static variables of the enclosing class
    What is diff between static class and a final class
    which all method are static in java?a static inner class is linke any other class in java
    a final class with static methods is like the java.lang.Math class - you cnnot subclass it and do normally not instantiate it
    hope that helps
    Harry

  • Question about static context when using classes

    hey there.
    i'm pretty new to java an here's my problem:
    public class kreise {
         public static void main (String[] args) {
             //creating the circles
             create a = new create(1,4,3);
             create b = new create(7,2,5);
             System.out.println(a);
             System.out.println(b);
             //diameters...unimportant right now
             getDiameter a_ = new getDiameter(a.radius);
             getDiameter b_ = new getDiameter(b.radius);
             System.out.println(a_);
             System.out.println(b_);
             //moving the circles
             double x = 2;
             double y = 9;
             double z = 3;
             a = create.move();
             System.out.println(a);
    }creating a circle makes use of the class create which looks like this:
    public class create {
        public double k1;
        public double k2;
        public double radius;
        public create(double x,double y,double r) {
            k1 = x;
            k2 = y;
            radius = r;
        public create move() {
            k1 = 1;
            k2 = 1;
            radius = 3;
            return new create (k1,k2,radius);
        public String toString() {
        return "Koordinaten: "+k1+" / "+k2+". Radius: "+radius;
    }now that's all totally fine, but when i try to usw create.move to change the circles coordinates the compiler says that the non-static method move() can't be referenced from a static context. so far i've seen that my main() funktion MUST be static. when declaring the doubles k1, k2, and radius in create() static it works, but then of course when having created the second circle it overwrites the first one.
    i pretty much have the feeling this is very much a standard beginner problem, but searching for the topic never really brought up my problem exactly. thanks in advance!

    You can't access a non-static method from within a static context. So, you have to call move() from outside of the main method. main has to be static because, in short, at least one method has to be static because there haven't been any objects initialized when the program is started. There are more fundamental problems than with just the static context issue.
    I'm confused by your code though. You call create.move(), but this would only be possible if move() was static, and from what I see, it's not. Now that's just one part of it. My second issue is that the logic behind the move() method is very messy. You shouldn't return a newly instantiated object, instead it should just change the fields of the current object. Also as a general rule, instance fields should be private; you have them as public, and this would be problematic because anything can access it.
    Have you heard of getters and setters? That would be what I recommend.
    Now, also, when you are "moving" it, you are basically creating a new circle with completely differently properties; in light of this, I've renamed it change(). Here would be my version of your code:
    public class CircleTester {
        public static void main (String[] args)
             //Bad way to do it, but here's one way around it:
             new CircleTester().moveCircle();
        private void moveCircle()     //really a bad method, but for now, it'll do
            Circle a = new Circle(1,4,3);
            Circle b = new Circle(7,2,5);
            System.out.println(a);
            System.out.println(b);
            //diameters. Don't need to have a new getDiameter class
            double a_ = a.getRadius() * 2;     //Instead of doing * 2 each time, you could have a method that just returns the radius * 2
            double b_ = b.getRadius() * 2;
            System.out.println(a_);
            System.out.println(b_);
            //move the circle
            a.change(2,9,3);
            System.out.println(a);
    public class Circle {
        private double k1;
        private double k2;
        private double radius;
        public Circle(double x,double y,double r)
            k1 = x;
            k2 = y;
            radius = r;
        public void change(int x, int y, int r)
            k1 = x;
            k2 = y;
            radius = r;
        public String toString()
             return "Koordinaten: "+k1+" / "+k2+". Radius: "+radius;
        public double getRadius()
             return radius;
    }On another note, there is already a ellipse class: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/Ellipse2D.html

  • Clarification about static variables declaration

    I'm getting "Illegal Start of Expression" error, When i try to declare a static variable within a static metho or nonstatic method.
    Could you please clarify me,
    Is it possible to declare a static varible within a static method or non static method ?
    Thanks

    Hi Vikas,
    First, note that this forum is devoted to Sun Java Studio Creator IDE. General Java questions can be asked on forums here: http://forum.java.sun.com/
    Second, note that static variable can be defined only as member of class. It cannot be defined inside of method.
    Thanks, Misha
    (Creator team)

  • A basic question about static variables and methods

    What would be the effects if I declare lots of static variables and static methods in my class?
    Would my program run faster?
    Would the class take more memory spaces? (If so, how do I estimate an appropriate number of variabls and methods to be declared static?)
    Thank you @_@

    when you declare a static var the var isn't created for every instance of the class, it just "live" in the class
    when you declare a static method is the same, so if you have:
    class MyClass
    static int myMethod()
    //Method work
    you dont need to have a instance of the class to call myMethod, you can do such things like this.
    int value = Myclass.myMethod();
    if myMethod isn't static you can't do this.. so, if
    class MyClass
    int myMethod()
    //Method work
    you CAN'T call
    int value = MyClass.myMethod();
    instead, you have to write
    MyClass m;
    m = new MyClass();
    value = m.myMethod();

  • Question about static (+multithreading)

    Sample program for multithreading:
    cvidir\samples\utility\Threading\ThreadSafeVar\IncrementValue.prj
    Why all of global variables and functions are static ?
    regards
    Frog

    This is a CVI question and should be posted to the LabWindows/CVI forum instead. Static flags are used to tell the compiler that these global declarations are not used outside this code module (C file). It's good programming practice to declare global variables only used in one module as static.
    Best Regards,
    Chris Matthews
    National Instruments

Maybe you are looking for

  • Change PO while doing Goods Receipt

    Hi, We are getting GR data through inbound IDOC. We have a a requirement to change the PO while creating GR document. Scenario is like : We have ordered Material 'A' in the PO but we received an equivalent Material 'B'. So while processing the inboun

  • Explanation of Edge Detection of Digital Images

    Can anyone suggest the best links for the complete explanation of doing edge detection using java.

  • BPEL Fault

    Somebody could help me with that error. I deploy a process that use 1 WSDL Document(BinesAsignadosWSDL.wsdl), when i try to create a process instance the follow error is thrown BPEL Fault: {http://schemas.oracle.com/bpel/extension}remoteFault{code=HT

  • What is the pay scale structure??

    i want to know What is meant by payscale structure ?? in detail 

  • How do you add Serial Number in Sales AR Invoice

    How do we add Serial Numbers in AR Invoice.What is the query. Can anybody guide me with the procedure.