I find it incredible that you can ignore all of Romans 2 and 3 when reading chapter 4.
27 Where then is boasting? It is excluded. By what kind of law? Of works? No, but by a law of faith.
What kind of works is Paul speaking of? WORKS OF THE LAW. One of the major themes of Romans is salvation does not come through the law. Law brings wrath. No one can be saved through the works
of the law. Paul spends all of Romans 2 and 3 establishing that
all are under the law, and none are able to be saved through the works of the law. And in Romans 4, Paul is demonstrating this using Abraham as his example, showing that Abraham was credited with righteousness because He believed God, and not because his works merited salvation.
4 Now to the one who works, his wage is not credited as a favor, but as what is due. 5 But to the one who does not work, but believes in Him who justifies the ungodly, his faith is credited as righteousness,
Thus, what we see is that Paul is establishing that no one can earn salvation through works.
Now, the question becomes "Is James saying that we earn salvation through meritorious works?"
The first part of James 2 speaks about treating a rich man well and the poor man poorly, and then goes into a discussion of acting in a moral manner. Then:
14 What use is it, my brethren, if someone says he has faith but he has no works? Can that faith save him?
Are we talking about
works that merit salvation, here? NO! We're talking about works that
result from our faith.
15 If a brother or sister is without clothing and in need of daily food, 16 and one of you says to them, "Go in peace, be warmed and be filled," and yet you do not give them what is necessary for [their] body, what use is that? 17 Even so faith, if it has no works, is dead, [being] by itself.
Notice the analogy. The person pays lip service to the poor and hungry, but does nothing about it. Likewise, if we believe, but don't act upon our belief, what good is it? It's not good for anything, just like telling a poor person to be warm and well fed.
18 But someone may [well] say, "You have faith and I have works; show me your faith without the works, and I will show you my faith by my works." 19 You believe that God is one. You do well; the demons also believe, and shudder. 20 But are you willing to recognize, you foolish fellow, that faith without works is useless?
And James exposes intellectual assent as a failure in terms of faith. The demons know who God is. But they don't place their FAITH in Him. Putting your faith in someone or something means that you act like you believe what they day. Your actions reflect your beliefs.
21 Was not Abraham our father justified by works when he offered up Isaac his son on the altar? 22 You see that faith was working with his works, and as a result of the works, faith was perfected; 23 and the Scripture was fulfilled which says, "And Abraham believed God, and it was reckoned to him as righteousness," and he was called the friend of God.
Notice the order, here. Abraham was credited with righteousness, and THEN his actions reflected his belief when he went to offer up Isaac, and that faith in God was justified when Isaac was spared.
24 You see that a man is justified by works and not by faith alone. 25 In the same way, was not Rahab the harlot also justified by works when she received the messengers and sent them out by another way?
Thus, what James is speaking of isn't
actions that merit salvation, as Paul speaks of in Romans 2-4, but James is speaking of the nature of true faith, one that performs works consistent with what it believes in.
Thus, when you take a proper exegetical look at these two passages, you see that James and Paul are speaking of the same faith, the same gospel, but speaking of two different sides of works. Paul is referring to works before salvation, which are unable to merit salvation for us, and James is speaking of works that are post-salvation, which ought to reflect the faith that we have embraced, which do not save us, but are the evidence of what we believe.
Muz