S
S
Shady10102016-04-03 13:55:16
Java
Shady1010, 2016-04-03 13:55:16

What is an Object in java?

Hello. I want to understand, in the book on java it is written that Object object is a variable of type Object, and then with the construction "= new Object()" we specify a reference to the object itself and our variable will store a reference to the object (instance), but often on the network there are something like this: Path path = Paths.get("C:\test.txt"); and it is also written "To create a Path object, there is an auxiliary class java.nio.file.Paths, which contains a method for obtaining the Paths.get path" with such a construction, do we create an object? and why a Path object is created, since the link will point to Paths and not Path. Help please, otherwise I'm already confused.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kornachev, 2016-04-03
@Shady1010

There is a Paths class, it has a static method that returns a new Path object. In this case, we do not have a reference to the Paths object, since we did not create an instance of it. Through the construction Paths.get("C:\test.txt"); we are accessing a static method that is available without instantiating objects.
Read about static methods and about the static keyword in general.
Here is an example code for this method.

public static Path get(String path){
  //создаем ссылку на объект Path
  Path pathObj = new Path();
  //проводим манипуляции по настройке и инициализации
  pathObj.setPath(path);
  //тут может быть куча проверок
  //куча других процедур

  //а после всех подготовок и проверок возвращаем ссылку на ранее созданный объект
  return pathObj;
}

T
Timur Sergeevich, 2016-04-03
@MyAlesya

Inside the method, you can create a new object and return it.

A a = B.get();

class B {
public static A get(){
return new A();
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question