Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Accessing java member variable that has the name "type"

I am using a java library in which a class has a member named "type". If I do something like this:

class MyClass{
  public MyClass(){
    type = 5;
  public int type;

then the java compiler compiles it fine. But if I try to access it from scala:

val x = new MyClass()
x.type = 10

I get this message:

identifier expected but 'type' found.

How do I work around this issue?

I am guessing this has come up before but I could not find a related question.

like image 758
mushroom Avatar asked Mar 23 '23 15:03


1 Answers

You can do this in scala by using backticks:

x.`type` = 10

Any string enclosed in backticks can be used as an identifier, or to access one.

like image 172
Shadowlands Avatar answered Apr 03 '23 02:04