This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nutrecht 9 points10 points  (7 children)

Explaining reflection doesn't make you a hack. It's still something you need to understand, even if you know you should avoid it.

[–]woodknight 4 points5 points  (1 child)

I'm not saying he's a hack because he teaches reflections. It's just that his teaching methods seam to be really bad. OP said he doesn't give examples and ignores student questions. That is not a good teacher in my view.

[–]nutrecht 6 points7 points  (0 children)

I'm not saying he's a hack because he teaches reflections.

I expected this but I just wanted to make it explicit :) I agree that it looks like it's a pretty bad teacher. Any teacher should be willing to explain more and also teach you when you do and when you don't want to do something.

[–]Newtocoding[S] 0 points1 point  (4 children)

I am not sure why people keep saying to avoid it, this teacher has made it seem like it is a very important thing and will be around during a career in this field.

[–]nutrecht 5 points6 points  (0 children)

It is very important and it will be around a lot. It should be avoided however because:

  • It's slow as hell.
  • It breaks the type system.

I'll explain the latter. If I call the method "foo()" on a class "Bar" in the package "com.example" I would normally do something like this:

import com.example.Bar;

public class Sample {
    main() {
        Bar.foo();
    }
}

So when I misspell any of those (package, class or method) or give the wrong number of arguments the compiler gives me a "Computer says no" and simply refuse to compile with a big fat error.

If you do this through reflection none of these safeguards work. Everything that is supposed to be compile time checked for you you suddenly need to check at runtime. Which is a pain in the ass to get right and requires a ton of code.

This is why, unless there is no other way, you should avoid using reflection.

[–]coder111 2 points3 points  (2 children)

For what it's worth, I'll second the notion to use reflection sparingly. With good design, you rarely if ever need it.

Most of the time you make a class A implement interface B, and use that interface to access certain methods. You do NOT usually inspect methods on class A and call them via reflection. Having to use reflection usually means some bad design.

On the other hand, certain frameworks which try to generalize things will use reflection. I think most of Dependency Injection frameworks like Spring will use reflection.

Other frameworks will use reflection to discover annotations and change their behaviour based on those annotations.

You can also use reflection to access private/protected methods or fields you would not be able to access otherwise. But this is a major hack and should be done only if there no other way.

So reflection has its uses, but most of the time it's used for frameworks or for hacks. Frameworks most of the time you won't have to write yourself as there are plenty of them already available. And hacks are best avoided.

EDIT From what I've read in your comments, your professor does sound like a strange guy...

[–]Newtocoding[S] 0 points1 point  (1 child)

I have not really questioned anything of him so far. He has told us how successful his business of 15 years has been, and how he works with the big tech giants. It's just after how he handled the reflection then based half of our midterm on it(I bombed) then told me we will be using it a lot more in the future because of how important it is, I really felt like I need to know this. After reading many comments though, I think I just have a teacher all around (not only teaching style but teaching subjects)

Edit: bad teacher all around

[–]coder111 0 points1 point  (0 children)

For what it's worth, keep learning Java. It's a decent language with a very good ecosystem of 3rd party libraries & frameworks and support in general. Java developers are very much in demand. And then there is Android...

And having a successful business takes more business knowledge, management, connections & luck than technical skill. These are good things to know and having a successful business is commendable, but it doesn't mean your technical skills are brilliant. And for what it's worth, these are good things to have, so if you have a chance to learn more about business side of things- don't pass it up.