static

C++关键字static的用法

孤人 提交于 2020-01-14 08:52:07
转载: https://www.cnblogs.com/songdanzju/p/7422380.html 四个作用: 1.先来介绍它的第一条也是最重要的一条:隐藏。(static函数,static变量均可) 当同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性。 2.static的第二个作用是保持变量内容的持久。(static变量中的记忆功能和全局生存期) 存储在静态数据区的变量会在程序刚开始运行时就完成初始化,也是唯一的一次初始化。共有两种变量存储在静态存储区:全局变量和static变量,只不过和全局变量比起来,static可以控制变量的可见范围,说到底static还是用来隐藏的。虽然这种用法不常见 PS:如果作为static局部变量在函数内定义,它的生存期为整个源程序,但是其作用域仍与自动变量相同,只能在定义该变量的函数内使用该变量。退出该函数后, 尽管该变量还继续存在,但不能使用它。 3.static的第三个作用是默认初始化为0(static变量) 其实全局变量也具备这一属性,因为全局变量也存储在静态数据区。在静态数据区,内存中所有的字节默认值都是0x00,某些时候这一特点可以减少程序员的工作量。比如初始化一个稀疏矩阵,我们可以一个一个地把所有元素都置0,然后把不是0的几个元素赋值。如果定义成静态的,就省去了一开始置0的操作

AS3 TypeError: Error #1007: Instantiation attempted on a non-constructor

♀尐吖头ヾ 提交于 2020-01-14 07:51:12
问题 For some reason I can't get this to work (heavily simplified code that fails): package com.domain { public class SomeClass { private static var helper:Helper = new Helper(); } } class Helper { } It compiles, but throws upon first access of SomeClass : TypeError: Error #1007: Instantiation attempted on a non-constructor. at com.domain::SomeClass$cinit() ... 回答1: +1 to Darren. Another option is to move the Helper class to the top of the file class Helper { } package com.domain { public class

java的修饰符

对着背影说爱祢 提交于 2020-01-14 07:29:15
Java的修饰符根据修饰的对象不同分为: 1.类修饰符 2.方法修饰符 3.变量修饰符 其中每种修饰符又分为 访问控制修饰 符和 非访问控制修饰符 1. 类修饰符 访问修饰符:公共类修饰符public(只有一种) 非访问控制符:抽象类修饰符 abstract 、最终类修饰符 final (1)公共类修饰符 public : Java 语言中类的访问控制符只有 public 即公共的。每个 Java 程序的有且只有一个类是 public,它被称为主类 , 其他外部类无访问控制修饰符 ,具有包访问性。 public class Test1 {} class Test2{} abstract class Test3{} final class Test4{} 注意:一个类的内部类可以被其他访问控制修饰符protected、default、private修饰,相当于类的成员。 (2)抽象类修饰符 abstract :用 abstract 修饰符修饰的类,被称为抽象类,在抽象类里不能创建实例,只能当成父类被继承。 (3)最终类修饰符 final :当一个类不能被继承时可用修饰符 final修饰为最终类。被定义为 final 的类通常是一些有固定作用、用来完成某种标准功能的类。 (4)类缺省访问控制符:如果一个类没有访问控制符,说明它具有缺省的访问控制符特性。此时

Java never declares access level of variables in methods whether static or not [closed]

孤街醉人 提交于 2020-01-14 07:12:58
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . Today I suddenly realized that Java never specifies access level for local variables when I try to declare a variable for main method. I also tried to give it a access level modifier, but it doesn't compile. The

Java never declares access level of variables in methods whether static or not [closed]

。_饼干妹妹 提交于 2020-01-14 07:12:11
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . Today I suddenly realized that Java never specifies access level for local variables when I try to declare a variable for main method. I also tried to give it a access level modifier, but it doesn't compile. The

How to access static methods of generic types

岁酱吖の 提交于 2020-01-14 07:11:16
问题 public class BusinessObjects<O> where O : BusinessObject { void SomeMethod() { var s = O.MyStaticMethod(); // <- How to do this? } } public class BusinessObject { public static string MyStaticMethod() { return "blah"; } } Is there a correct object oriented approach to accomplishing this or will I need to resort to reflection? EDIT: I went too far in trying to oversimplify this for the question and left out an important point. MyStaticMethod uses reflection and needs the derived type to return

Alternatives to static and global in C++?

 ̄綄美尐妖づ 提交于 2020-01-14 07:11:11
问题 I have a class instance that needs to be accessed by some other classes. It would be quite cumbersome to pass the instance always down the construction chain. I tried to avoid global variable, since people tend to advise against this. I thought I declare this instance a static member of a class and then include this class in order to access the instance but this does not work either error: calling a private constructor of class 'Foo' To specify the problem further in the context of the

Spring Security Thymleaf static resources don't load

时光总嘲笑我的痴心妄想 提交于 2020-01-14 05:54:09
问题 I'm using SpringMVC with Thymleaf and Spring-Security. I want to load a page using Thymleaf template and I can load my static resources. I want to load for example a picture located in : static/img/theme/logo.png from template.html Here is what I have : result template.html : body> div layout:fragment="content"> a href="">img src="../static/img/theme/logo.png" alt="Logo"> h1>Hello /div> /body> MvcConfig.java @Configuration public class MvcConfig extends WebMvcConfigurerAdapter { @Override

Limiting concurrent access to a method

风流意气都作罢 提交于 2020-01-14 04:45:13
问题 I have a problem with limiting concurrent access to a method. I have a method MyService that can be called from many places at many times. This method must return a String , that should be updated according to some rules. For this, I have an updatedString class. Before getting the String , it makes sure that the String is updated, if not, it updates it. Many threads could read the String at the same time but ONLY ONE should renew the String at the same time if it is out of date. public final

What is RIWO (Read Indirectly Write Out) state

让人想犯罪 __ 提交于 2020-01-14 03:41:25
问题 I was reading about the static flow control and came across the RIWO concept. Can someone explain this with simple terminology and perhaps a code sample? This is related to the error "Illegal forward reference". Relevant link. 回答1: After going through some material and discussing with couple of guys offline i found out the following information. When a java class is getting executed there are few steps which JVM performs few steps sequentially. Identify the static members from top to bottom.