static-methods

How to access a Java static method from Scala given a type alias for that class it resides in

为君一笑 提交于 2019-12-03 06:00:24
Given the type-alias type Cal = java.util.Calendar how can the static getInstance method be accessed? I tried the following in Scala REPL: scala> type Cal = java.util.Calendar defined type alias Cal scala> Cal.getInstance <console>:8: error: not found: value Cal Cal.getInstance ^ scala> val Cal = java.util.Calendar <console>:7: error: object Calendar is not a value val Cal = java.util.Calendar ^ Is import java.util.{Calendar => Cal} followed by import Cal._ really my best bet? You can't. Yes, import java.util.{Calendar => Cal} is really your best bet. Conceptually, Scala object members are

Why does a static constructor not have any parameters?

非 Y 不嫁゛ 提交于 2019-12-03 05:42:47
Per MSDN: A static constructor does not take access modifiers or have parameters. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. A static constructor cannot be called directly. Can any one please explain why can't the static constructor have parameters? Motti As MSDN says, A static constructor is called automatically to initialize the class before the first instance is created . Therefore you can't send it any parameters. If the CLR must call a static constructor how will it know which parameters

Java no name static method

久未见 提交于 2019-12-03 05:40:27
问题 What is this? public class ABC { public ABC() { System.out.println("world"); } static { System.out.println("hello"); } } Will print: hello world I don't really understand this, or what kind of method that static code is. 回答1: It's called a "static initialisation block". It runs when the class is first loaded; only once. For example, a constructor will run each time the class is instantiated; the static block only runs once, when it's first loaded statically by the VM/Class loader. 回答2: I

What is the use of private static member functions?

强颜欢笑 提交于 2019-12-03 05:30:42
问题 I was looking at the request parser from the boost::asio example and I was wondering why the private member functions like is_char() are static ? : class request_parser { ... private: static bool is_char(int c); ... }; It is used in the function consume which is not a static function: boost::tribool request_parser::consume(request& req, char input) { switch (state_) { case method_start: if (!is_char(input) || is_ctl(input) || is_tspecial(input)) { return false; } ... Only member functions can

public static void main () access non static variable

故事扮演 提交于 2019-12-03 05:11:52
问题 Its said that non-static variables cannot be used in a static method.But public static void main does.How is that? 回答1: No, it doesn't. public class A { int a = 2; public static void main(String[] args) { System.out.println(a); // won't compile!! } } but public class A { static int a = 2; public static void main(String[] args) { System.out.println(a); // this works! } } or if you instantiate A public class A { int a = 2; public static void main(String[] args) { A myA = new A(); System.out

How to call a static method from a private base class?

故事扮演 提交于 2019-12-03 04:29:40
Due to the layout of a third-party library, I have something like the following code: struct Base { static void SomeStaticMethod(){} }; struct Derived1: private Base {}; struct Derived2: public Derived1 { void SomeInstanceMethod(){ Base::SomeStaticMethod(); } }; int main() { Derived2 d2; d2.SomeInstanceMethod(); return 0; } I'm getting compiler error C2247 with MSVC: Base::SomeStaticMethod not accessible because Derived1 uses private to inherit from Base. I know I can't access Base members from Derived2 via inheritance because of the private specifier, but I should still be able to call a

No enclosing instance of type is accessible. [duplicate]

我只是一个虾纸丫 提交于 2019-12-03 03:29:00
This question already has answers here : Java - No enclosing instance of type Foo is accessible (5 answers) The whole code is: public class ThreadLocalTest { ThreadLocal<Integer> globalint = new ThreadLocal<Integer>(){ @Override protected Integer initialValue() { return new Integer(0); } }; public class MyThread implements Runnable{ Integer myi; ThreadLocalTest mytest; public MyThread(Integer i, ThreadLocalTest test) { myi = i; mytest = test; } @Override public void run() { System.out.println("I am thread:" + myi); Integer myint = mytest.globalint.get(); System.out.println(myint); mytest

What is the right way to use an injected bean in a static method?

烈酒焚心 提交于 2019-12-03 03:09:26
This question might seem a little odd. Suppose I have a Service which I want to use in a Utility class that has some static methods. The Service is a Spring bean, so naturally I will for example use a setter and (@Autowired) to inject it into my utility class. As it is mentioned in Spring's documentation, all beans are static in the bean context. So when you want to inject a bean in a class you don't have to use "static" modifier. See below: public class JustAClass{ private Service service; public void aMethod(){ service.doSomething(....); } @Autowired public void setService(Service service){

When to make a method static?

蓝咒 提交于 2019-12-03 02:14:45
问题 I'd like to know how people decide whether to define a method as static. I'm aware that a method can only be defined as static if it doesn't require access to instance fields. So let's say we have a method that does not access instance fields, do you always define such a method as static, or only if you need to call it statically (without a reference to an instance). Perhaps another way of asking the same question is whether you use static or non-static as the default? 回答1: I use static

Why use classmethod instead of staticmethod? [duplicate]

对着背影说爱祢 提交于 2019-12-03 01:16:26
This question already has answers here : What is the difference between @staticmethod and @classmethod? (23 answers) I know what they do and I've seen many examples of both, but I haven't found a single example where I would have to use classmethod instead of replacing it with a staticmethod . The most common example of classmethod I've seen is for creating a new instance of the class itself, like this (very simplified example, there's no use of the method atm. but you get the idea): class Foo: @classmethod def create_new(cls): return cls() This would return a new instance of Foo when calling