Accessing private method from a static one - flutter

I'm trying to access a private method from a static one in the same class in Dart.
class MyClass{
void _initFunc() {
/// ...
}
static void info(){
if (condition){
_initFunc();
}
}
}
I got this error
Instance members can't be accessed from a static method.
Can you please explain me why, and how can i do. info() must be static, and _initFunc() must be private.

There is no "this" in a static method. So MyClass.info() doesn't have a "this" to call this._initFunc();
In Dart the static modifier on class members and methods makes them available without creating an instance of a class object. For example special constructors are static because you want to create a instance with them. Consider MyClass foo = MyClass.fromAnotherObject(bar); -> static MyClass fromAnotherObject(){} is static because you don't yet have a MyClass object to call it on.
In your example code, you could change _initFunc() to a public function (remove the "_") and either:
a) instantiate a MyClass object inside your static info() method and call initFunc()
class MyClass{
void initFunc() {
/// ...
}
static void info(){
if (condition){
MyClass myClass = MyClass();
myClass.initFunc();
}
}
}
OR
b) declare initFunc() also static and call it from info()
class MyClass{
static void initFunc() {
/// ...
}
static void info(){
if (condition){
MyClass.initFunc();
}
}
}

Related

Dart: Inherit some of the public methods privately

I want to inherit a class, but I want to make some of its methods private, which allows mutation of the object. I do not want to override those methods, as that will make them still accessible and cause a runtime error. I want something like this:-
class BaseClass {
//...
void update() {/*Implementation*/}
void read() {/*Implementation*/}
}
class ChildClass extends BaseClass {
//...
void read() {/*Implementation*/}
}
int main() {
final first = BaseClass(), second = ChildClass();
first.update(); //Works
second.update(); //Error: Method does not exist.
}
I don't think what you want it possible. The entire point of extending classes is that child classes can access all public methods of the superclass. You might want to use composition instead. That would be something like:
class BaseClass {
//...
void update() {/*Implementation*/}
void read() {/*Implementation*/}
}
class ChildClass {
BaseClass _baseClass
//...
void read() { _baseClass.read() }
}

Java program compilation failed

While studying interface I came along this weird behaviour
When I am running this
int num=20;
public void sound();
public void eat();
}
class Dog implements Animal{
public void sound(){
System.out.println("Wooof!!!!!!!");
}
public void eat(){
System.out.println("Food");
}
}
public class Main{
public static void main(String[]args){
Dog dog=new Dog();
dog.sound();
dog.eat();
System.out.println(Dog.num);
//System.out.println(Dog.num1);
}
}
It runs fine while If I declare a no static variable with same name i.e. num as of the one in interface like this
interface Animal{
int num=20;
public void sound();
public void eat();
}
class Dog implements Animal{
int num=10;
public void sound(){
System.out.println("Wooof!!!!!!!");
}
public void eat(){
System.out.println("Food");
}
}
public class Main{
public static void main(String[]args){
Dog dog=new Dog();
dog.sound();
dog.eat();
System.out.println(Dog.num);
//System.out.println(Dog.num1);
}
}
It gives this error Main.java:22: error: non-static variable num cannot be referenced from a static context
My question was since one from interface is static and is of class level why the child class i.e. Dog compilation fails when I declare a non static instance level variable.
The problem is your are trying to access Dog class into System.out.println(). Not the variable dog where the class has been initialized.
If variable num is not static is impossible to read it without initialize the class, so if you want to do Dog.num, that variable has to be static.
If you don't add the variable into Dog class, the value will be declared in the interface, but if the variable exists in the class, the compiler will try read that.

Kotlin abstract method with body

As mention here: if a function in an interface has no body it is abstract by default. But there is nothing about interface's function with body.
Example:
interface MyInterface {
fun foo() { print("Something") }
fun bar()
}
fun main(args: Array<String>) {
println(MyInterface::foo.javaMethod)
println(MyInterface::bar.javaMethod)
}
Output will be:
public abstract void MyInterface.foo()
public abstract void MyInterface.bar()
How it's possible, that method with defined body is abstract?
This has to do with the way default methods in Kotlin interfaces are implemented. The foo and bar methods in your interface really are both abstract.
However, there is an inner class inside the interface that looks something like this (simplified):
public interface MyInterface {
void foo();
void bar();
public static final class DefaultImpls {
public static void foo() {
System.out.print("Something");
}
}
}
This class is the one that contains the default implementations of any functions that you gave a body to inside the interface.
Then, if you create a class that implements this interface, and you don't override the foo method:
class MyClass: MyInterface {
override fun bar() {
println("MyClass")
}
}
Then you get one generated automatically, which just calls the implementation inside DefaultImpls:
public final class MyClass implements MyInterface {
public void bar() {
System.out.println("MyClass");
}
public void foo() {
MyInterface.DefaultImpls.foo();
}
}
You can find all these details by using the bytecode viewer that comes with the Kotlin plugin (Tools -> Kotlin -> Show Kotlin Bytecode, and then the Decompile option).

What is the difference between a private static function vs. a public static function in typescript?

In the context of an angular2 service; what is the difference between a private static function vs. a public static function in typescript?
public static getUserStockList(): Stock[] {
/* TODO: implement http call */
return WATCHLIST;
}
vs.
private static getUserStockList(): Stock[] {
/* TODO: implement http call */
return WATCHLIST;
}
EDIT:
When would it be appropriate to use a private static function over a private function?
Private static methods can be invoked from instances of the class.
An example of this:
interface Data {
// ...
}
interface StrictData {
// ...
abstract class MyClass {
protected constructor(data: StrictData) {
// ...
}
}
class AnotherClass extends MyClass {
private static normalizeData(data?: Data | StrictData): StrictData {
// ...
}
constructor(data?: Data | StrictData) {
super(AnotherClass.normalizeData(data));
}
}
AnotherClass.normalizeData is accessible from the instance and the compiler is just fine with this.
However, this:
console.log(AnotherClass.normalizeData({}));
Will result in:
Property 'normalizeData' is private and only accessible within class
'AnotherClass'

Calling Partial Methods in C#

i was recently digging on new partial methods in c#3.0, i understood the use of partial class, that it could be chunked into multiple file one contain the definition and other declaration, but i wanted to know,i created a partial class like below:
in class1.cs
partial class A
{
partial void Method();
}
in class2.cs
partial class A
{
partial void Method()
{
Console.WriteLine("Hello World");
}
}
now in class3.cs
class MainClass
{
static void Main()
{
A obj = new A();
obj.Method(); //Here i cannot call the "Method" method.
}
}
then whats the use of creating partial method, i read on MSDN that, at runtime, compiler compiles the class into one, in that case compiler should be getting the "Method" method implementation also, then why it dont allow me to call the "Method" method in the main method, can anyone correct me if i am wrong, and tell me why i am unable to call this partial method in main.
From MSDN
No access modifiers or attributes are allowed. Partial methods are
implicitly private.
It's a private method, so you can't call it from main.
You can call a partial method inside the constructor where the method is defined.
For example
public partial class classA
{
partial void mymethod();
}
public partial class classA
{
partial void mymethod()
{
Console.WriteLine("Invoking partial method");
}
public ClassA()
{
mymethod();
}
}
public class MainClass
{
static void Main()
{
ClassA ca=new ClassA();
}
}
That's it..now execute your code and see the result..
OutPut
Invoking partial method
Yes, we can't call it from Main(). Problem is not Partial method problem is method without specifier in a class is Private and private method can be called inside the class only.
Try creating a new public method in Partial class:
partial class A
{
partial void Method();
}
partial class A
{
partial void Method()
{
Console.WriteLine("Hello World");
}
public void Study()
{
Console.WriteLine("I am studying");
Method();
}
}
class MainClass
{
static void Main()
{
A obj = new A();
obj.Study();
}
}