companion-object Questions

5

Solved

I try to write a class to manage a SQLite DB, but I have the error message "Expecting a top level declaration > Task :app:buildInfoGeneratorDebug". package com.xexxxwxxxxs.GMP import android.da...
Hyman asked 6/3, 2019 at 7:9

3

Solved

I'm pretty new to kotlin and I was wondering if it's possible, and if it's against best practice to access methods and variables that are outside a companion object, from within the companion objec...
Accordingly asked 3/4, 2017 at 7:59

2

Solved

I read this statement: By using User.getClass, you are referring to the class companion object that Scala by default creates for the case class, and not the case class itself. To get the class obj...
Spermatophore asked 30/1, 2023 at 15:42

2

Solved

In Kotlin language, what does this syntax do and how does it work ? class ClassName1 { companion object { fun ClassName2.funName()="" } }
Caraway asked 7/6, 2022 at 14:22

6

Solved

How can we access application context inside companion object in Android kotlin? I have a companion object inside an abstract class and I want to access context to read Shared Preferences, but I'm ...
Milka asked 7/1, 2019 at 13:48

4

Solved

Simple sample class with companion object class MyClass { companion object { val test = 25 } } The value test can be accessed from by MyClass.test, but how to access from an instance of MyClass...
Lodie asked 19/10, 2017 at 9:44

2

Solved

i want to use BottomNavigationView in my app and i'm facing this problem with kotlin (never had it before with java) i see this message : classifier 'listFragment' does not have a companion object ...
Liturgy asked 13/8, 2018 at 22:45

1

Solved

Scala 2.13 I have tons of similar traits of the form trait SomeTrait[F[_]]{ def someOp(): F[Unit] //... } and their implementations class SomeTraitImpl[F[_]: Sync] extends SomeTrait[F]{ //....

6

Solved

What is the difference between an object and a companion object in a class in kotlin? Example: class MyClass { object Holder { //something } companion object { //something } } I already...
Phan asked 5/5, 2017 at 22:29

2

I learn the use of ViewHolder from an offical sample named UserViewHolder. public class UserViewHolder extends RecyclerView.ViewHolder { static UserViewHolder create(LayoutInflater inflater, Vie...
Antimasque asked 11/1, 2018 at 5:21

1

Here's the interface: interface SomeInterface { companion object { const val MY_CONST = "the constant" } } And then, the class (without body because is only an example): class SomeClass : So...
Pragmatic asked 11/8, 2018 at 0:58

1

Solved

How can I have the scala compiler automatically generate the case object? // Pizza class class Pizza (val crust_type: String) // companion object object Pizza { val crustType = "crust_type" } ...
Judithjuditha asked 26/6, 2018 at 17:16

2

Solved

I am facing an issue with Companion Objects picking up its type instead of the case class I am using spray json serdes. They need an implicit JsonFormat. This format is obtained by calling a func...
Cosy asked 2/3, 2018 at 17:0

1

Solved

I would like to be able to access the simpleName of my class from it's companion object. I would like this: val o1 = Outer("foo") val o2 = Outer("bar") to print the following output: Outer: he...
Upheaval asked 31/1, 2018 at 2:15

1

Solved

Given the following code: open class Foo { companion object { fun fez() {} } } class Bar : Foo() { companion object { fun baz() { fez() } } } baz() can call fez() I can call Foo.fe...
Mistrustful asked 14/11, 2017 at 22:14

7

Solved

Is there a case where a companion object (singleton) for a class is needed? Why would I want to create a class, say Foo and also create a companion object for it?
Flue asked 4/3, 2009 at 8:46

3

I have a mixed project, Java and Kotlin classes, and I want to know how I can refer to companion objects from my Java classes.
Loraineloralee asked 25/5, 2017 at 20:16

1

Consider following code: object Main extends App { object Project { case class Config(rules: Seq[String] = Seq.empty) } import Project._ //case class Project(root: String, config: Config) /...
Comeuppance asked 8/4, 2017 at 11:58

1

Solved

I have an Activity class DummyActivity : Activity() { companion object { @JvmStatic fun onNewIntent(context: Context): Intent { val intent = Intent(context, DummyActivity.javaClass) return in...
Helvetia asked 1/4, 2017 at 7:26

3

While trying to understand companion objects, I have written following code which counts the number of times a class was instantiated. I had to use a 'var' to keep the count. Is there a 'functional...
Supportive asked 30/11, 2016 at 19:17

2

Solved

Is it possible to initialize an companion object before the init block in a Kotlin class? If so, how? If not, is there a way to accomplish the same thing. I have the following scenario, class A(...
Sexivalent asked 31/10, 2016 at 11:21

1

I have a controller in my project which has a socket method I want to call that method in companion object.But somehow i am not able to do that as i need to pass parameters also to companion object...
Neary asked 1/7, 2016 at 9:31

3

Solved

I have written this methods in Kotlin and analysed the bytecode: Situation 1 class A { object b { fun doSomething() {} } } Situation 2 class A { companion object b { fun doSomething() {} ...
Taliataliaferro asked 24/6, 2016 at 20:10

1

Solved

I am trying to refactor some models I have that currently look like this: case class Person(name: String, age: Int) object Person { implicit val reads: Reads[Person] = ( (JsPath \ "name").read[...
Rabon asked 18/4, 2016 at 2:15

2

Solved

Many classes in the Scala standard library use apply() of their companion object as factory. This is often convenient when chaining calls like List(List(1)). On the other hand, it's still possible ...
Wentz asked 28/8, 2015 at 20:48

© 2022 - 2024 — McMap. All rights reserved.