static-members

C++ static const variable and destruction

Hi, I have encountered a strange behavior with a simple C++ class. classA.h class A { public: A(); ~A(); static const std::string CONST_STR; }; classA.cpp #include "classA.h" #include <cassert> const std::string A::CONST_STR("some text"); A::A() { assert(!CONST_STR.empty()); //OK } A::~A() { assert(!CONST_STR.empty());...

What is the lifetime of class static variables in C++ ?

If I have a class called Test :: class Test { static std::vector<int> staticVector; }; when does staticVector get constructed and when does it get destructed ? Is it with the instantiation of the first object of Test class, or just like regular static variables ? Just to clarify, this question came to my mind after reading Conce...

How to get System.Type instance of a class-type in Static Member ?

Hi, I have a public static property in a class. The class has some custom attributes applied to it. I want to access the Attribute in a static property. In a non-static member I can get the type of current class using this.GetType() but how do I do this in a static member of the class ? Please note that.. I do not want to use typeof...

Static member does not keep assigned value

OK I have a static class that has two static members, a string and a boolean. A public static method assigns values to these members based upon the state of parameters passed in. A private static method is then called that processes the static members. The problem is that while the boolean keeps the value it is set to in the public ...

Why might a static data member not get initialized?

I'm trying to register a bunch of classes with a factory at load time. My strategy is to harness static initialization to make sure that before main() begins, the factory is ready to go. This strategy seems to work when I link my library dynamically, but not when I link statically; when I link statically, only some of my static data me...

Get value of static field

I've got the following class: public static class Pages { public static string LoggedOut = "LoggedOut.aspx"; public static string Login = "Login.aspx"; public static string Home = "Home.aspx"; } I know I can use Pages.Home statically, but there is a reason for my question. I wish to have a method that I can call like this...

Objective c - static members and constants

Whats the difference between: @interface SomeClass : NSObject { NSObject *something; } and @interface SomeClass : NSObject { } NSObject *something; ? Also, what's the difference between Java's final and Objective C (C)'s const? And where should I declare static class members for the following situations: 1. When only the clas...

Question about Scope of Static Class Variables in Java

Hi Folks, I have a static object defined in my logging class, along the lines of: class myLoggingClass { static java.util.Properties properties; ... ... } According to my reference book, this means that the properties object is shared by all instances of my class. I find this definition insufficient. I'm writin...

What are static and dynamic variables / methods in OOP?

I am trying to better understand basic concepts in OOP. What are static and dynamic variables and methods in object-oriented programming? What is, for instance, the difference between using $this vs. double colon (::)? $this ($this->a_method()) Advantages: ?. Disadvantages: ? ... "this" is not self-documenting as in: $this->method_fr...

Best way to get a static DateTime value

Is there a better way to get a static DateTime value in C# than the following? public static DateTime StartOfRecordedHistory = DateTime.Parse("2004-01-01"); Thanks! ...

Static Method Calls

Hello All, Does each static call will initiate a new thread? For E.g.: class A { public static void displayName() { Console.WriteLine("myName"); } public static void displayAge() { Console.WriteLine("myAge"); } } class B { public void Foo() { A.displayName(); A.display...

Python Scoping/Static Misunderstanding

I'm really stuck on why the following code block 1 result in output 1 instead of output 2? Code block 1: class FruitContainer: def __init__(self,arr=[]): self.array = arr def addTo(self,something): self.array.append(something) def __str__(self): ret = "[" for item in sel...

Static properties in PHP

Hello everyone. When are static properties initialized, as I know other member properties are initialized when object is created. Thanks ...

C++: static member functions and variables- redefinition of static variable?

I was trying to incorporate the Singleton design pattern into my code, but I started getting a weird error: main.obj : error LNK2005: "private: static class gameState * gameState::state" (?state@gameState@@0PAV1@A) already defined in gameState.obj If you're not familiar with the singleton pattern, it is basically used to enforce only ...

c++: Private constructor means no definition of that classes objects inside headers?

Yet another question, go me!... Anyway, I have 2 classes with private constructors and static functions to return an instance of that class. Everything was fine, I have a main.cpp file where I managed to get hold of my gameState object pointer, by doing: gameState *state = gameState::Instance(); But now I seem to have a problem. For t...

C++ Static member initalization (template fun inside)

Hello all, For static member initialization I use a nested helper struct, which works fine for non templated classes. However, if the enclosing class is parameterized by a template, the nested initialization class is not instantiated, if the helper object is not accessed in the main code. For illustration, a simplified example (In my ca...

java static vs non-static using this and event handlers

Hello, I'm trying to learn about java's event handlers and keep getting errors with type type (static/non-static) methods I create. Some code I'm trying to write looks like: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Main extends JFrame implements ActionListener{ static priva...

Usage of Static variables in Java Servlets (e.g. in AppEngine)

I have an application where Servlet has a method called Update(ReqIn, ReqOut). I call it from doGet & doPost and pass the Request and Response variables, and it is then up to Update(...) to fill out the following static variables: ... public class Server extends HttpServlet { public static HttpServletRequest In = null; public s...

C++ Duplicate Symbol error when defining static class variable in XCode

I have a static class member incremented in the constructor. As per the rules, it is declared in the class and defined outside. This should be totally legal. Any ideas why I'm getting a duplicate symbol error? class Player { private: static int numPlayers; public: Player() { numPlayers++; } }; int Player::numPlayers =...

Why does Java prohibit static fields in inner classes?

class OuterClass { class InnerClass { static int i = 100; // compile error static void f() { } // compile error } } Although it's not possible to access the static field with OuterClass.InnerClass.i, if I want to record something that should be static, e.g. the number of InnerClass objects created, it would be helpful to make th...