static-members

How to serialize static data members of a Java class?

烂漫一生 提交于 2019-11-26 08:18:36
问题 When we serialize objects, static members are not serialized, but if we need to do so, is there any way out? 回答1: The first question is why you need to serialize the static members? Static members are associated with the class, not the instances, so it does not make sense to include them when serializing an instance. The first solution is to make those members not static. Or, if those members are the same in the original class and the target class (same class, but possibly different runtime

How to force a static member to be initialized?

本小妞迷上赌 提交于 2019-11-26 08:16:26
问题 Consider this example code: template<class D> char register_(){ return D::get_dummy(); // static function } template<class D> struct Foo{ static char const dummy; }; template<class D> char const Foo<D>::dummy = register_<D>(); struct Bar : Foo<Bar> { static char const get_dummy() { return 42; } }; (Also on Ideone.) I\'d expect dummy to get initialized as soon as there is a concrete instantiation of Foo , which I have with Bar . This question (and the standard quote at the end) explained

Why do members of a static class need to be declared as static? Why isn&#39;t it just implicit?

ぃ、小莉子 提交于 2019-11-26 07:41:24
问题 Obviously there can\'t be an instance member on a static class, since that class could never be instantiated. Why do we need to declare members as static? 回答1: I get asked questions like this all the time. Basically the question boils down to "when a fact about a declared member can be deduced by the compiler should the explicit declaration of that fact be (1) required, (2) optional, or (3) forbidden?" There's no one easy answer. Each one has to be taken on a case-by-case basis. Putting

How to initialize static variables

為{幸葍}努か 提交于 2019-11-26 06:26:06
I have this code: private static $dates = array( 'start' => mktime( 0, 0, 0, 7, 30, 2009), // Start date 'end' => mktime( 0, 0, 0, 8, 2, 2009), // End date 'close' => mktime(23, 59, 59, 7, 20, 2009), // Date when registration closes 'early' => mktime( 0, 0, 0, 3, 19, 2009), // Date when early bird discount ends ); Which gives me the following error: Parse error: syntax error, unexpected '(', expecting ')' in /home/user/Sites/site/registration/inc/registration.class.inc on line 19 So, I guess I am doing something wrong... but how can I do this if not like that? If I change the mktime stuff with

How to initialize static variables

泄露秘密 提交于 2019-11-26 03:26:47
问题 I have this code: private static $dates = array( \'start\' => mktime( 0, 0, 0, 7, 30, 2009), // Start date \'end\' => mktime( 0, 0, 0, 8, 2, 2009), // End date \'close\' => mktime(23, 59, 59, 7, 20, 2009), // Date when registration closes \'early\' => mktime( 0, 0, 0, 3, 19, 2009), // Date when early bird discount ends ); Which gives me the following error: Parse error: syntax error, unexpected \'(\', expecting \')\' in /home/user/Sites/site/registration/inc/registration.class.inc on line 19

Why does Java prohibit static fields in inner classes?

好久不见. 提交于 2019-11-26 03:18:10
问题 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 that field static. So why does Java prohibit static fields/methods in inner classes? EDIT: I know how to make the compiler happy with static nested class (or static

Error message Strict standards: Non-static method should not be called statically in php

自古美人都是妖i 提交于 2019-11-26 03:10:39
问题 I have the following php. However when I see the index.php I get the following error message. Strict standards: Non-static method Page::getInstanceByName() should not be called statically in /var/www/webworks/index.php on line 12 I am hoping someone can tell me how to fix the problem. index.php // { common variables and functions include_once(\'ww.incs/common.php\'); $page=isset($_REQUEST[\'page\'])?$_REQUEST[\'page\']:\'\'; $id=isset($_REQUEST[\'id\'])?(int)$_REQUEST[\'id\']:0; ... // { get

Android static object lifecycle

孤者浪人 提交于 2019-11-26 01:41:34
问题 I am creating event search application, we set search criteria from one screen populate in another screen then user can edit search criteria from 3rd screen and goes to 4th screen. To achieve above task i am using static object which remember the values around the application and i don\'t need to do any thing extra. But i am afraid if about static object life cycle in android if low memory found android delete static objects ??? As android supports multi tasking, if user switches to another

Undefined reference to static constexpr char[]

烂漫一生 提交于 2019-11-26 00:20:57
问题 I want to have a static const char array in my class. GCC complained and told me I should use constexpr , although now it\'s telling me it\'s an undefined reference. If I make the array a non-member then it compiles. What is going on? // .hpp struct foo { void bar(); static constexpr char baz[] = \"quz\"; }; // .cpp void foo::bar() { std::string str(baz); // undefined reference to baz } 回答1: Add to your cpp file: constexpr char foo::baz[]; Reason: You have to provide the definition of the

Are static fields open for garbage collection?

社会主义新天地 提交于 2019-11-25 23:37:24
问题 Given an hypothetical utility class that is used only in program setup: class MyUtils { private static MyObject myObject = new MyObject(); /*package*/static boolean doStuff(Params... params) { // do stuff with myObject and params... } } will myObject be garbage collected when it is no longer being used, or will it stick around for the life of the program? 回答1: Static variables cannot be elected for garbage collection while the class is loaded. They can be collected when the respective class